Python-检查列表中是否有字母
如果列表中有字母(字符串), 查找字母(['o',['hello','c','bye']), 返回True,如果不返回FalsePython-检查列表中是否有字母,python,string,list,recursion,Python,String,List,Recursion,如果列表中有字母(字符串), 查找字母(['o',['hello','c','bye']), 返回True,如果不返回False def find_letter(lst): lst=['o','hello', 1] n='o' if not lst: return 0 elif lst[0] == n: return True elif find_letter(lst[0:]):
def find_letter(lst):
lst=['o','hello', 1]
n='o'
if not lst:
return 0
elif lst[0] == n:
return True
elif find_letter(lst[0:]):
return True
else:
return False
print(find_letter(lst))
它确实返回“True”,但我不确定这是否是正确的方法。也许有更好的方法吗?在第二个elif语句中,如果第一个元素不包含字母,python是否会遍历列表中的所有元素?函数必须是递归的。我认为最类似python的方法应该是使用
def find_letter(letter, lst):
return any(letter in word for word in lst)
它的美妙之处在于它在lst
上迭代,并且只要列表中的一个单词包含字母
,它就会返回。此外,它不需要递归
如果
lst
为空,则返回False
而不是0
,不过(与您的程序不同),但由于False
的计算结果为0
(反之亦然),这并不是一个真正的问题。您想找到成员身份吗
请参考此代码以解决您的问题。
假设
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]
print 3 in list2
输出:
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
True
刚刚意识到OP只想检查一个字符串 您可以定义函数并递归地匹配列表中的字符串,如下所示:
def plist(lst, n):
# loop inside the list
for each in lst:
# if "each" is also a list (nested), check inside that list, too
if isinstance(each, list):
# this will reuse your "plist" and loop over any nested list again
plist(each, n)
# if n matches any element in the list, return True
if any(n in each_letter for each_letter in each):
return True
# if after looping over your list + nested list, return False if no match is find
else:
return False
>> lst = ['o', ['hello', 'c', 'bye']]
>> plist(lst, 'o')
>> True
>> plist(lst, 'h')
>> True
>> plist(lst, 'z')
>> False
希望这能解决您的问题。这是您的错误
def find_letter(lst): # You receive your list as lst
lst=['o','hello', 1] # Opppsss! You override it. It does not matter what you receive with lst above, now its value is ['o','hello', 1]
n='o'
因此,在find_letter(lst[0:])
中,您使用列表切片,但在lst=['o','hello',1]
行中,您再次覆盖它,并且始终在列表的第一个元素上执行搜索
n = "o" # you can set this too, but this is optional
def find_letter(lst):
# do not set a value to lst in here
if not lst:
return 0
elif lst[0] == n: # You checked first element in here
return True
elif find_letter(lst[1:]): # since you checked the first element, skip it and return the orher elements of the list
return True
else:
return False
lst = ['o','hello', 1]
print find_letter(lst)
因为您需要递归版本: 短版 更明确的版本 更明确的版本
请注意,我省略了几个
else:
,因为它们在return
语句之后是不必要的。如果您不想测试字符串中的字母,而只是为了相等,请将let in…
替换为let==…
如果第一个元素不存在,python会遍历列表中的所有元素吗包含字母-您可以使用print
语句来确认您自己:-)欢迎使用stackoverflow,您可以修复缩进吗?list
是python中的一个类型,因此它是一个内置的。不要将其用作变量名,否则您将覆盖现有的python类型。您的递归函数看起来很好,但我没有这是一个简单的错误。在elif find_letter(lst[0:])
中,您为find_letter
函数提供了相同的列表。您必须跳过第一个元素(零索引元素)并返回其他元素。您必须正确使用列表切片。@FallenAngel我尝试过(lst[1:)]和(lst[:-1]),没有一个有效。不幸的是,OP刚刚编辑了他的问题,添加了“函数必须是递归的”!@Ben:哦,运气不好。很感谢你强迫学生在根本不需要递归的示例上学习递归。除非我们处理的是嵌套列表。
def find_letter(let, lst):
return (lst or False) and \
((isinstance(lst[0], str) and let in lst[0]) or find_letter(let, lst[1:]))
def find_letter(let, lst):
if lst:
e = lst[0]
return (isinstance(e, str) and let in e) or find_letter(let, lst[1:])
return False
def find_letter(let, lst):
if lst:
e = lst[0]
if isinstance(e, str) and let in e:
return True
return find_letter(let, lst[1:])
return False