wrong output in matching elements of a list of list in python -


i have list of list in python sample data looks this:

list_of_list = = [['ab2768', 'new york city', '25.0'], ['ab1789', 'san francisco', '38.0'], ['ab6783', 'chicago', '7.0'], ['ab2897', 'new york city', '30.0']] 

what have passing 2 parameters function - id, city. in function matching if bot parameters match return third value else return 0. here code far:

def match_records(id, city):     list_of_list = [['ab2768', 'new york city', '25.0'], ['ab1789', 'san francisco', '38.0'], ['ab6783', 'chicago', '7.0'],['ab2897', 'new york city', '30.0']]       enrollment = ''      print("searching id- " + str(id))     print("searching city- " + str(city))     idnum, cityname, val in list_of_list:         print(idnum + ', ' + cityname + ', ' + val)         if str(idnum.strip()) != '' , str(id.lower().strip()) != str(idnum.lower().strip()) , str(                 city.lower().strip()) not in str(cityname.lower().strip()):             print('either id empty or id not found or city not found')             flag = 1          else:             print('found mactch')             flag = 0             enrollment = val             break      return (flag == 0, enrollment) 

if print(match_records('ab2768', 'san francisco')) ideally should false ab2768 , san francisco not in same list getting true. in fact if either of 2 inputs correct returning true. know error somewhere in if logic unable guess is. mistake here?

you can use "truthy" statements in code:

def match_records(id, city):     list_of_list = [['ab2768', 'new york city', '25.0'], ['ab1789', 'san francisco', '38.0'], ['ab6783', 'chicago', '7.0'], ['ab2897', 'new york city', '30.0']]      new_list = [i in list_of_list if id in , any(city in b b in i)]     if new_list:           return new_list[0][-1]     else:           return 0   print(match_records('ab2768', 'san francisco')) 

in example, code looks of sublists contain both id , city. however, if both not found in same sublist, sublist not added enter list in comprehension. thus, if no match found, empty list created. in python, empty list evaluates false, returning boolean give false.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -