想找一下某个list中list的值,例如,example_list=[["a","male","34 years old"],["b","female","26 years old"]]
现在我想def一个function,用来寻找客户a是否在这个list中,如果在的话,会得到True,应该怎么做?
def check_username(username,exemple_list)
check_username(a,[["a","male","34 years old"],["b","female","26 years old"]])
True
是否是使用index来运行?
能够通过def一个function来判断目标客户是否在list之中,如果在的话,得到True
def search(l,s):
for i in l:
for j in i:
if j==s:
return True
return False
example_list=[["a","male","34 years old"],["b","female","26 years old"]]
print(search(example_list,'a'))
example_list=[["a","male","34 years old"],["b","female","26 years old"]]
new_list=[i[0] for i in example_list] #这里面存的是所有的客户姓名
if 'a' in new_list: #判断客户a是否在list中
#do something
用好列表推导式,就2句代码你都不用声明函数
直接return ('a' in example_list)
def search(n,l):
return n in sum(l,[])
example_list = [["a", "male", "34 years old"], ["b", "female", "26 years old"]]
print(search('a',example_list))