Compare response and name list in experiment - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Compare response and name list in experiment (/thread-28458.html) |
Compare response and name list in experiment - knoxvillerailgrind - Jul-20-2020 Hi, i´m rewriting a program for a psychological experiment. The existing code only accepts the response in a certain length. The first part is about lowering the name and response for comparison and hasSubstring is a new variable, is it? I´ve tried learning the functions len, range, if and for but i cant put them together in this code.. Anyone can explain to me how the code works and how i rewrite it to just compare the response with name? Thanks! ___prepare__ def checkIfContains(name, response): name = name.lower(); response = response.lower(); stringFragments = [] for i in range(0,len(response)-3): stringFragments.append(response[i:i+5]) hasSubstring = False; for i in range(0,len(stringFragments)-1): if stringFragments[i] in name: hasSubstring = True; return hasSubstring __end__ RE: Compare response and name list in experiment - perfringo - Jul-20-2020 What does 'just compare the response with name?' mean? RE: Compare response and name list in experiment - knoxvillerailgrind - Jul-26-2020 * A list consisting movies, typos and so on in order to recognize a certain celebrity (e.g. Brad Pitt; Bred Pidd; Troy...) RE: Compare response and name list in experiment - deanhystad - Jul-26-2020 I can see where this code would be confusing since it contains errors and what it does is really odd. def checkIfContains(name, response): name = name.lower(); response = response.lower(); # This code creates a bunch of substrings, all 5 characters in length, # except the last substring that is only 4 characters in length because # the range is wrong Should be "for i in range(len(response)-4)" stringFragments = [] for i in range(0,len(response)-3): stringFragments.append(response[i:i+5]) hasSubstring = False; # This error fixes the previous error by ignoring the last # substring. Should be # for substr in stringFragments: # if substr in name: # return True # return False for i in range(0,len(stringFragments)-1): if stringFragments[i] in name: hasSubstring = True; return hasSubstringI do not understand what the code is supposed to do and I don't think this logic does it. The code creates a bunch of 5 character long strings and returns True if any of those substrings are in name. If you are trying to do a near match (match strings that are close to the same), take a look at difflib. There are also a lot of python libraries used in natural language processing that handle this problem (look for "string distance") |