2016-11-06 11 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']] 

私は単語が1つのリストにソートしたい:私はそれらをインポートしたい場合でも、私はこのリストを取得

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

は、これは私のpythonのコードです

amazement, awe, bombshell, curiosity, incredulity, 
    godsend, marvel, portent, prodigy, revelation, 
    stupefaction, unforeseen, wonder, shock, rarity, 
    miracle, abruptness, astonishment 
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.") 

は、私は言葉でテキストファイルを持っています

ご協力いただきありがとうございます。

答えて

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。 、

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) 

又はその後者の場合、おそらく使用:その後者の場合

、わずかコンマに従って分割を実行し、空の結果の文字列(if word)をドロップするために、このようなgencompでforを追加regex splitは良い(私たちは空の文字列も破棄する必要がある)。式を空白またはカンマで区切ります。

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()はリストを生成するので、結果に追加するとリストが得られます。 ソリューションは、あなたがそれを

0

使用

lst.extend(i) 

の代わり

を分割する前に、私をストリッピングすることにより 'の\ r \ n' を取り除くことができます2リスト(+)

を連結することであろう

lst.append(i) 

splitはリストを返し、毎回リストを追加します。代わりにextendを使用すると、問題が解決します。

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.") 
関連する問題