gpt4 book ai didi

python - 简单合并文件的最快方法是什么?拆分数组的最快方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:07 24 4
gpt4 key购买 nike

获取文件列表和输出文件名称并将它们合并到一个文件中同时删除重复行的最快方法是什么?像

cat 文件1 文件2 文件3 |排序-u > out.file

在 python 中。

最好不要使用系统调用。

和:

将 python 中的列表尽可能平等地拆分为 X 个 block (列表列表)的最快方法是什么? (给定一个列表和 X。)

最佳答案

首先:

lines = set()
for filename in filenames:
with open(filename) as inF:
lines.update(inF)
with open(outfile, 'w') as outF:
outF.write(''.join(lines))

第二个:

def chunk(bigList, x):
chunklen = len(bigList) / x
for i in xrange(0, len(bigList), chunklen):
yield bigList[i:i+chunklen]

listOfLists = list(chunk(bigList, x))

关于python - 简单合并文件的最快方法是什么?拆分数组的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3893696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com