我对Python非常陌生,我只是想了解一些事情
我得到了一堆要打开的txt文件,其中包含数千个单词,全部用空格分隔,我相信我在下面的代码中已经涵盖了这一点。。但我只是想知道如何将这些行分解成3块或用户输入的一些int
从['perfect', 'dealing', 'crave', 'stirring', 'expedition', 'alexandria', 'vanish', 'dealing', 'crave']
到[['perfect', 'dealing', 'crave'], ['stirring', 'expedition', 'alexandria'],['vanish', 'dealing', 'crave']]
甚至回到列表的形式
a
b
c
短暂性脑缺血发作
word_list=[]
filename = "filename.txt"
with open(filename,"r") as file_object:
for line in file_object:
word_list=line.split()
print(word_list)
return(word_list)
使用itertools配方中的
grouper
构造:您的代码是:
您可以使用
for i in range(0, len(word_list), 3)
在间隔为3的列表上迭代,并使用索引对原始列表进行切片您不是在更新
word_list
,而是过度写入了它的值,它应该是这样的相关问题 更多 >
编程相关推荐