Python - 检查一个字母是否在列表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26355191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - check if a letter is in a list
提问by raspberrysupreme
If a letter (string) is in a list, find_letter(['o', ['hello', 'c', 'bye']), return True, if not return False.
如果一个字母(字符串)在一个列表中,find_letter(['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:]):
return True
else:
return False
print(find_letter(lst))
It does return 'True', but I am not sure if this is the right way to do this. Maybe there's a better way? In the second elif-statement, is python going through all the elements in the list if the first one doesn't contain the letter? The function must be recursive.
它确实返回“True”,但我不确定这是否是正确的方法。也许有更好的方法?在第二个 elif 语句中,如果第一个不包含字母,python 是否遍历列表中的所有元素?该函数必须是递归的。
采纳答案by FallenAngel
Here is your error
这是你的错误
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'
So in find_letter(lst[0:]), you use list slicing, but on lst=['o','hello', 1]line, you override it again and you always execute your search on the first element of the list.
因此,在 中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)
回答by Tim Pietzcker
I think the most pythonic approach would be to use
我认为最pythonic的方法是使用
def find_letter(letter, lst):
return any(letter in word for word in lst)
The beauty of this is that it iterates over lstand returns as soon as one of the words in that list contains letter. Also, it doesn't need to recurse.
这样做的好处在于,只要lst该列表中的一个单词包含,它就会迭代并返回letter。此外,它不需要递归。
This returns Falseinstead of 0if lstis empty, though (unlike your program) but since Falseevaluates to 0anyway (and vice versa), that's not really an issue.
这种回报False,而不是0如果lst是空的,但(不像你的程序),但由于False计算结果为0反正(反之亦然),这不是一个真正的问题。
回答by Sohair Ahmad
You want to find the Membership
你想找到会员
Please refer this code to resolve your problem. Suppose
请参考此代码来解决您的问题。 认为
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
Output:
输出:
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
True
回答by Anzel
Just realized OP wants to check A stringonly You can define a function and match the string in a list recursively like this:
刚刚意识到 OP 只想检查A 字符串您可以定义一个函数并递归匹配列表中的字符串,如下所示:
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
Hope this solves your problem.
希望这能解决您的问题。
回答by uselpa
Since you need a recursive version:
由于您需要递归版本:
Short version
精简版
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:]))
More explicit version
更明确的版本
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
Even more explicit version
更明确的版本
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
Note that I leave out a couple of else:because they are not necessary after a returnstatement. If you don't want to test for a letter in a string, but just for equality, replace let in ...by let == ....
请注意,我省略了几个,else:因为在return声明之后它们不是必需的。如果您不想测试字符串中的字母,而只想测试相等性,请替换let in ...为 let == ...。

