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
如果在迭代什么是True
:
any([False, True, False]) # returns True
谷歌也有很好的信息。为了构建这个迭代器,我使用了一个叫做generator expression
的东西。
- 它通过循环列表:
for issue in lol.split()
- 做出一个布尔值:
issue.lower() in pyscaldamage
- 移动到下一个项目
因此,这种形式的样本生成器表达式可能是这样的:
my_gen = (x == 2 for x in [1, 2, 3]) # a generator expression
注意它在括号内。如果你打开一个控制台它看起来somethign像这样:
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())
- 如果所以,没有东西
什么是您的提示输入字符串? – Andy
你打印的问题的价值,并且哈哈 - 他们有道理吗?在if和else else语句中添加一个打印语句,该语句打印'lol not found' - 现在你能更好地理解发生了什么?变量问题的目的是什么? – barny
如果你永远不会使用它,你为什么要计算'issue'? – inspectorG4dget