2013-07-15 71 views
0

我試圖讓我的程序從文本文件中抓取每第五個單詞並將其放在單個字符串中。例如,如果我輸入「每個人都喜歡吃餡餅,因爲它的味道非常好,並且它有許多品種,比如藍莓草莓和酸橙」,那麼該程序應該打印出「所有人,因爲加上品種和。」。我必須從第一個單詞開始,然後每隔五分鐘抓一個單詞。我很困惑如何做到這一點。下面是我的代碼,除了最後5行外,一切都運行正常。Python 3.3:如何抓取文本文件中的每個第5個單詞?

#Prompt the user to enter a block of text. 
done = False 
textInput = "" 
while(done == False): 
    nextInput= input() 
    if nextInput== "EOF": 
     break 
    else: 
     textInput += nextInput 

#Prompt the user to select an option from the Text Analyzer Menu. 
print("Welcome to the Text Analyzer Menu! Select an option by typing a number" 
    "\n1. shortest word" 
    "\n2. longest word" 
    "\n3. most common word" 
    "\n4. left-column secret message!" 
    "\n5. fifth-words secret message!" 
    "\n6. word count" 
    "\n7. quit") 

#Set option to 0. 
option = 0 

#Use the 'while' to keep looping until the user types in Option 7. 
while option !=7: 
    option = int(input()) 

#I'm confused here. This is where I'm stuck. Is the 'for' loop correct for this `#situation?` 
#If the user selects Option 5, 
    elif option == 5: 
     for i in textInput.split(): 
      if i <= 4 and i >= 6: 
       print(textInput) 
+1

忽略其他問題,我很好奇你什麼時候''i'既是'<= 4' *也是*'> = 6'。 – user2357112

回答

2

使用您定義字與str.split(),下面的任一會做的方法,你想要什麼:

textInput = """\ 
I'm trying to have my program grab every fifth word from a text file and 
place it in a single string. For instance, if I typed "Everyone likes to 
eat pie because it tastes so good plus it comes in many varieties such 
as blueberry strawberry and lime" then the program should print out 
"Everyone because plus varieties and." I must start with the very first 
word and grab every fifth word after. I'm confused on how to do this. 
Below is my code, everything runs fine except the last 5 lines.""" 

everyfive = ' '.join(word for i,word in enumerate(textInput.split()) if not i%5) 

# or more succinctly 
everyfive = ' '.join(textInput.split()[::5]) 

print(repr(everyfive)) 

無論哪種方式,輸出將是:

"I'm program from place string. typed pie good many strawberry program because 
must first fifth on Below runs 5" 

越短使用[::5]符號的(因此更快更簡單)版本基於所謂的「切片」,所有序列在Python中都支持。一般概念在documentation附近序列部分開頭附近描述。

0

split()的輸出是字符串中的單詞列表。例如: -

>>> "The quick brown fox jumped over the lazy dog and then back again".split() 
['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog', 'and', 
'then', 'back', 'again'] 
>>> 

因此,爲了讓每一個第五字:

>>> for i,s in enumerate("The quick brown fox jumped over the lazy dog and then 
back again".split()): 
...  if i%5 == 0: print (s) 
... 
jumped 
and 
>>>> 
+0

用戶需要第一個字,然後每五分鐘,所以在零索引時,你會希望詞0,5,10等。應該是如果我%5 == 0 – thumbtackthief

+1

@thumbtackthief:謝謝。我同意我錯過了這一點。我已編輯更正。 – Simon

0

您可以用空格分開的句子,然後由5遞增數組的索引來獲得期望的結果。

textInput = "Everyone likes to eat pie because it tastes so good plus it comes in many varieties such as blueberry strawberry and lime" 
steps = 5 
words = textInput.split() 
for x in xrange(1, len(words), steps): 
    print words[x] 

#OUTOUT 
Everyone 
because 
plus 
varieties 
and 
2

for i in textInput.split()環比在textInput的話,而不是指數。如果你想同時指數和的話,你想

for i, word in enumerate(textInput.split()): 

我不知道當時的想法是落後i <= 4 and i >= 6什麼,因爲這些條件不能同時是真實的。如果你想選擇的每一個第五字,你想

if i % 5 == 0: 

,檢查是否在將i通過5餘數是0

但是,您根本不需要if語句。你可以將分割後的列表切片得到每5個元素:

# Iterate over every 5th word in textInput. 
for word in textInput.split()[::5]: 
    print(word) 
0

這是我的基本解決方案。我相信有人會說這不是'pythonic',但它完成了工作。

someString = "Everyone likes to eat pie because it tastes so good plus it comes in many varieties such as blueberry strawberry and lime" 
someList = someString.split() 
loadString = '' 
i = 0 
for s in range(len(someList)): 
    if i < len(someList) - 1: 
     loadString += someList[i] + ' ' 
     i += 5 
print loadString.rstrip(' ') 
相關問題