2016-03-15 120 views
-1

我想分割一個輸入,看看輸入是否在我所做的任何列表中,但是它不輸出任何東西。請幫忙。使用拆分功能

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

什麼是您的提示輸入字符串? – Andy

+0

你打印的問題的價值,並且哈哈 - 他們有道理嗎?在if和else else語句中添加一個打印語句,該語句打印'lol not found' - 現在你能更好地理解發生了什麼?變量問題的目的是什麼? – barny

+0

如果你永遠不會使用它,你爲什麼要計算'issue'? – inspectorG4dget

回答

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 如果在迭代什麼是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())
  • 如果所以,沒有東西
+0

謝謝Ben。代碼確實有效。但是我不明白髮生了什麼事。如果我要使用這個,我必須解釋這個代碼。 –

+0

@Bruhthenewyearjokes - 然後選擇一種可以解釋的方法。有一些提供的答案只是與你的最小差異,可能是可以解釋的。這種方法非常好(+1),涵蓋了很多情況,但可能會a)對您的問題過於矯枉過正和(b)難以解釋。 – MSeifert

+0

@MSeifert我已經嘗試了所有其他人,他們似乎沒有工作。我已經調整了Ben的代碼,但是我理解你的觀點。 –

-1

不要使用input,使用raw_input代替。

注爲Python 3.x的

raw_input如在評論中所指出改爲input在Python 3.X(仍然可以作爲eval(input()))。

+2

Python 3中不是這樣的。 – Andy

+0

它創建了文件,但它不打印k?有什麼東西可能會衝突的代碼? –

-1

你的問題是你if語句中的issue considerationinstead採取lol(這是你的lolsplit())。如果你嘗試的東西的線

if issue in pyscaldamage: 
    fix = open('pyscaldamage.txt','w') 
    print('K') 

它應該工作。

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 
1

你所面臨的問題是,你是否lol(即輸入字符串)在您的列表中。不是!

你可能想檢查是否有任何的特定的詞(這些是那些你在issue保存)是在列表中:

for string in issue: 
    if string in pyscaldamage: 
     print('K')