這是我的代碼:files = open('clean.txt').readlines()print filesfinallist = []for items in files: new = items.split() new.append(finallist)由于文本文件太大,這里有一個“打印文件”的例子:files = ['chemistry leads outstanding another story \n', 'rhapsodic moments blow narrative prevent bohemian rhapsody']我真的需要用 '\n' 分隔的每一行被拆分成單詞并放在一個列表列表中,就像下面的格式:outcome = [['chemistry','leads','outstanding', 'another', 'story'],['rhapsodic','moments','blow', 'narrative', 'prevent', 'bohemian', 'rhapsody']]我已經嘗試過類似于給出的第一個代碼的方法,它返回一個空列表。請幫忙!提前致謝。
2 回答

Helenr
TA貢獻1780條經驗 獲得超4個贊
看來,您的代碼的最后一行是向后的。代替
new.append(finallist)
它應該是
finallist.append(new)
我把最后一行改成上面的版本,結果是一個包含2個子列表的列表(finallist)。這是似乎有效的代碼:
files = open('clean.txt').readlines()
print files
finallist = []
for items in files:
new = items.split()
finallist.append(new)
添加回答
舉報
0/150
提交
取消