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