我正在尝试拆分输入以查看输入是否在我创建的任何列表中,但它不会输出任何内容。请帮忙。
pyscaldamage =['Casing','casing','Screen','screen','water','wet','Water','Wet','bad','Bad','Speakers','speakers,','Charger','charger','Buttons','buttons']
OSissue = ['crashed','Crashed','Slow','Slow','Freezing','freezing','Rebooting','rebooting','Loading','loading','fails','Fails']
phonesetup = ['Setup','setup','Email','email','WIFI','wifi','Bluetooth','bluetooth','Contacts','contacts','Icloud','icloud']
lol = input('What is the issue? ')
issue = lol.split()
if lol in pyscaldamage:
fix = open('pyscaldamage.txt','w')
print('K')
答案 0 :(得分:2)
pyscaldamage =['Casing','casing','Screen','screen','water','wet','Water','Wet','bad','Bad','Speakers','speakers,','Charger','charger','Buttons','buttons']
OSissue = ['crashed','Crashed','Slow','Slow','Freezing','freezing','Rebooting','rebooting','Loading','loading','fails','Fails']
phonesetup = ['Setup','setup','Email','email','WIFI','wifi','Bluetooth','bluetooth','Contacts','contacts','Icloud','icloud']
lol = input('What is the issue? ')
# Examine all the words in the splitted string
# if you lowercase them, the user's case (ScReeN) doesn't matter
# You can also make your searchlist only lowercase with this
if any(issue.lower() in pyscaldamage for issue in lol.split()):
print('k')
# This is a better way to open files because you dont have to remember
# to close them
with open('pyscaldamage.txt', 'w') as fix:
# do stuff
pass # get rid of this once you have stuff in the with statement
此方法使用any function。
any
函数采用迭代(暂时将其视为列表)并返回True
如果iterable中的任何内容为True
:
any([False, True, False]) # returns True
Google也有很好的信息。为了构建那个迭代,我使用了一个名为generator expression
的东西。
for issue in lol.split()
issue.lower() in pyscaldamage
因此,此表单的示例生成器表达式可能类似于:
my_gen = (x == 2 for x in [1, 2, 3]) # a generator expression
注意它在括号中。如果你打开一个控制台,它会看起来像这样:
In [2]: my_gen = (x == 2 for x in [1,2,3])
Out[2]: <generator object <genexpr> at 0x0000000009215FC0>
您可以致电next
:
In [7]: next(my_gen)
Out[7]: False # x == 1
In [8]: next(my_gen)
Out[8]: True # x == 2
In [8]: next(my_gen)
Out[9]: False # x == 3
如果你继续前进,它会对你大喊:
In[10]: next(my_gen)
Traceback (most recent call last):
File "<ipython-input-10-3539869a8d50>", line 1, in <module>
next(my_gen)
StopIteration
因此,正如您所看到的,您只能使用一次生成器表达式。生成器表达式是迭代的,
所以any
可以与他们合作。这段代码的作用是
lol.split()
for issue in lol.split()
issue.lower() in pyscaldamage
any(issue.lower() in pyscaldamage for issue in lol.split())
答案 1 :(得分:1)
您遇到的问题是您检查列表中是否有lol
(即输入的字符串)。它不是!
您可能想要检查列表中是否有任何特定字词(这些是您在issue
中保存的字词):
for string in issue:
if string in pyscaldamage:
print('K')
答案 2 :(得分:0)
如果您不关心lol
中pyscaldamage
的哪个令牌:
issue = lol.split()
if any(token in pyscaldamage for token in issue):
# do some generic stuff
否则:
issue = lol.split()
for token in issue
if token in pyscaldamage:
# do sth. with token
答案 3 :(得分:-1)
请勿使用input
,而是使用raw_input
。
请注意Python 3.x
raw_input
已在Python 3.x中更改为input
,如评论中所述(仍以eval(input())
提供)。
答案 4 :(得分:-1)
您的问题是if
语句正在考虑lol
,而不是issue
(即split()
lol
)。如果你尝试了
if issue in pyscaldamage:
fix = open('pyscaldamage.txt','w')
print('K')
它应该有用。