2016-11-06 18 views
2

我想在python中使用文本文件中的單詞而不是直接寫入python文件中的單詞(在這種情況下,代碼完美地工作)製作混雜遊戲。但是,當我想導入他們,我得到這個列表:在Python中使用讀取文本文件創建一個使用字符串的列表

[['amazement', ' awe', ' bombshell', ' curiosity', ' incredulity', '\r\n'], ['godsend', ' marvel', ' portent', ' prodigy', ' revelation', '\r\n'], ['stupefaction', ' unforeseen', ' wonder', ' shock', ' rarity', '\r\n'], ['miracle', ' abruptness', ' astonishment\r\n']] 

我想的話在一個單一的列表進行排序,例如:

["amazement", "awe", "bombshell"...] 

這是我的Python代碼:

import random 

#Welcome the player 
print(""" 
    Welcome to Word Jumble. 
     Unscramble the letters to make a word. 
""") 


filename = "words/amazement_words.txt" 

lst = [] 
with open(filename) as afile: 
    for i in afile: 
     i=i.split(",") 
     lst.append(i) 
print(lst) 

word = random.choice(lst) 
theWord = word 

jumble = "" 
while(len(word)>0): 
    position = random.randrange(len(word)) 
    jumble+=word[position] 
    word=word[:position]+word[position+1:] 
print("The jumble word is: {}".format(jumble)) 

#Getting player's guess 
guess = input("Enter your guess: ") 

#congratulate the player 
if(guess==theWord): 
    print("Congratulations! You guessed it") 
else: 
    print ("Sorry, wrong guess.") 

input("Thanks for playing. Press the enter key to exit.") 

我有文字的文本文件:

amazement, awe, bombshell, curiosity, incredulity, 
    godsend, marvel, portent, prodigy, revelation, 
    stupefaction, unforeseen, wonder, shock, rarity, 
    miracle, abruptness, astonishment 

謝謝你的幫助和任何建議!

回答

5

準一班輪做它:

with open("list_of_words.txt") as f: 
    the_list = sorted(word.strip(",") for line in f for word in line.split()) 

print(the_list) 
  • 使用在GEN-理解雙for
  • 對空間
  • 分裂是絕招:它擺脫了行終止字符和多重的空間。然後,使用strip()擺脫逗號。
  • 上所產生的發電機理解應用sorted

結果:

['abruptness', 'amazement', 'astonishment', 'awe', 'bombshell', 'curiosity', 'godsend', 'incredulity', 'marvel', 'miracle', 'portent', 'prodigy', 'rarity', 'revelation', 'shock', 'stupefaction', 'unforeseen', 'wonder'] 

只有這種快速方法的缺點是,如果2個字只用逗號隔開,它會發出2個字作爲-is。

在這後者的情況下,只是在這樣的gencomp添加for執行分裂根據逗號和丟棄空的結果字符串(if word):

with open("list_of_words.txt") as f: 
    the_list = sorted(word for line in f for word_commas in line.split() for word in word_commas.split(",") if word) 

print(the_list) 

或者在後者的情況下,也許使用正則表達式分割更好(我們也需要放棄空字符串)。拆分表達式爲空白或逗號。

import re 

with open("list_of_words.txt") as f: 
    the_list = sorted(word for line in f for word in re.split(r"\s+|,",line) if word) 
+0

一行程序的伎倆!感謝您的詳細解答和解答。 –

0

str.split()生成一個列表,所以如果將它附加到結果中,您將得到一個列表列表。 一個解決方案是串聯的2列表(+)

可以拆分它

0

使用

lst.extend(i) 

代替

之前擺脫 '\ r \ n' 的通過剝離我
lst.append(i) 

split返回一個列表,並且每次列出一個列表。使用擴展代替將解決您的問題。

1

請嘗試以下代碼:

import random 

#Welcome the player 
print(""" 
    Welcome to Word Jumble. 
     Unscramble the letters to make a word. 
""") 

name = " My name " 

filename = "words/amazement_words.txt" 

lst = [] 
file = open(filename, 'r') 
data = file.readlines() 

another_lst = [] 
for line in data: 
    lst.append(line.strip().split(',')) 
print(lst) 
for line in lst: 
    for li in line: 
     another_lst.append(li.strip()) 

print() 
print() 
print(another_lst) 

word = random.choice(lst) 
theWord = word 

jumble = "" 
while(len(word)>0): 
    position = random.randrange(len(word)) 
    jumble+=word[position] 
    word=word[:position]+word[position+1:] 
print("The jumble word is: {}".format(jumble)) 

#Getting player's guess 
guess = input("Enter your guess: ") 

#congratulate the player 
if(guess==theWord): 
    print("Congratulations! You guessed it") 
else: 
    print ("Sorry, wrong guess.") 

input("Thanks for playing. Press the enter key to exit.") 
相關問題