使用拆分功能

时间:2016-03-15 18:12:10

标签: python if-statement split

我正在尝试拆分输入以查看输入是否在我创建的任何列表中,但它不会输出任何内容。请帮忙。

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')

5 个答案:

答案 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 functionany函数采用迭代(暂时将其视为列表)并返回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
  • 询问此创建的iterable中的任何内容是否为True: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)

如果您不关心lolpyscaldamage的哪个令牌:

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')

它应该有用。