使用拆分功能
我想分割一个输入,看看输入是否在我所做的任何列表中,但是它不输出任何东西。请帮忙。使用拆分功能
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')
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())
- 如果所以,没有东西
谢谢Ben。代码确实有效。但是我不明白发生了什么事。如果我要使用这个,我必须解释这个代码。 –
@Bruhthenewyearjokes - 然后选择一种可以解释的方法。有一些提供的答案只是与你的最小差异,可能是可以解释的。这种方法非常好(+1),涵盖了很多情况,但可能会a)对您的问题过于矫枉过正和(b)难以解释。 – MSeifert
@MSeifert我已经尝试了所有其他人,他们似乎没有工作。我已经调整了Ben的代码,但是我理解你的观点。 –
不要使用input
,使用raw_input
代替。
注为Python 3.x的
raw_input
如在评论中所指出改为input
在Python 3.X(仍然可以作为eval(input())
)。
Python 3中不是这样的。 – Andy
它创建了文件,但它不打印k?有什么东西可能会冲突的代码? –
你的问题是你if
语句中的issue
considerationinstead采取lol
(这是你的lol
split()
)。如果你尝试的东西的线
if issue in pyscaldamage:
fix = open('pyscaldamage.txt','w')
print('K')
它应该工作。
如果你不关心它的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
你所面临的问题是,你是否lol
(即输入字符串)在您的列表中。不是!
你可能想检查是否有任何的特定的词(这些是那些你在issue
保存)是在列表中:
for string in issue:
if string in pyscaldamage:
print('K')
什么是您的提示输入字符串? – Andy
你打印的问题的价值,并且哈哈 - 他们有道理吗?在if和else else语句中添加一个打印语句,该语句打印'lol not found' - 现在你能更好地理解发生了什么?变量问题的目的是什么? – barny
如果你永远不会使用它,你为什么要计算'issue'? – inspectorG4dget