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
#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


Messages In This Thread
RE: Compare response and name list in experiment - by deanhystad - Jul-26-2020, 12:23 PM

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