我正在嘗試使用以下格式"a b c d e f g h"
來讀取文本文件。我拿一個新的空單word = []
我如何獲取列表而不是列表?
我的代碼是:
f = open("letters.txt")
word = []
for line in f:
line = line.split(" ")
word.append(line)
print(word)
但是,它給了我這樣的目錄列表:
[['a', 'b', 'c', 'd', 'e', 'f']]
但我想要得到它在單一列表呢?
如:
['a', 'b', 'c']
@RahulKP ^沒有,只有第一行的字。 –
你可能想澄清你的問題中有些令人困惑的術語。 '['a','b','c']'表明你想要一個字符列表,而我確定你想要一個*字詞列表*。另外* word *(而不是word ** s **)作爲列表出於同樣的原因混淆。 –
嗨PRnoob,請澄清^。問題現在還不清楚。 –