我是这里的新手,我正在尝试让python识别另一个内部的.split()列表。这有点难以解释,所以我会表明:
>>> #this is a function that separates the last word in the string, spliting the string by words and then transforming it into a list and the using the len() to know what is the position of the last item in the list, so that I can return it to another var.
>>> def lastWord(phrase):
phrase = list(phrase.split()[len(list(phrase.split()))-1:]))
return phrase
>>> x = "Hello World"
>>> d = lastWord(x)
>>> d
['World']
>>> x.split()
['Hello', 'World']
>>> one_list ["Hello", "World", "Anything"]
>>> one_list
['Hello', 'World', 'Anything']
>>> x.split() in one_list
False
>>> list(x.split()) in one_list
False
>>> d in one_list
False
如何让它识别使用.split()在另一个列表中创建的生成的拆分列表?
答案 0 :(得分:1)
问题是因为你试图在[" Hello"," World",&#]中找到[' Hello',' World'] #34;任何"]
时会给你真实的
one_list = [["Hello", "World"], "Anything"]
答案 1 :(得分:0)
更改以下内容 -
x.split() in one_list
到 -
exists = True
for item in x.split():
if item not in one_list:
exists = False
break
print exists
答案 2 :(得分:0)
你不能在"中使用"像那样测试每个单独的项目:
x.split() in one_list
试试这个:
all(x in one_list for x in x.split())