Python Forum
Compare response and name list in experiment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare response and name list in experiment
#1
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! Smile



___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__
Reply
#2
What does 'just compare the response with name?' mean?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
* A list consisting movies, typos and so on in order to recognize a certain celebrity (e.g. Brad Pitt; Bred Pidd; Troy...)
Reply
#4
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 hasSubstring
I 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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,191 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Compare two Excel sheets with Python and list diffenrences dmkfon 1 14,491 Oct-09-2021, 03:30 PM
Last Post: Larz60+
  Bode plot from time series experiment data discus 4 7,209 Jun-20-2020, 07:46 AM
Last Post: discus
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 2,848 May-10-2020, 05:28 AM
Last Post: deanhystad
  API JSON response missing list gives keyerror rolfmadsen 3 3,399 Mar-28-2020, 10:12 AM
Last Post: buran
  how to compare a list to a list of lists kevthew 1 1,774 Dec-22-2019, 11:43 AM
Last Post: ibreeden
  Optimise experiment control parameters rickticktock 2 2,457 Apr-02-2019, 10:54 AM
Last Post: rickticktock
  Compare element of list with line of file : if match, delete line silfer 4 3,477 Jul-21-2018, 02:44 PM
Last Post: silfer
  lab experiment / encryption pythan 0 2,436 Jun-09-2018, 07:19 PM
Last Post: pythan
  Compare dictionaries in list vaison 2 2,487 May-10-2018, 09:05 AM
Last Post: vaison

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020