首页 > 解决方案 > 如何使来自多个文件的大数据/代码的所有可能组合。代码超过200、400、1000、20亿

问题描述

我有大量的代码(代码数 = 9898654986.),即使有时它会在多个文件中(10、20、40....甚至 200 位)。我想对这些代码进行所有可能的组合,组合应该打印在多个文件上。但我想要所有可能的组合,例如如果代码 = ab1、aa2、dc3、xx4。所以组合应该像.. ('ab1', 'ab1'), ('ab1', 'aa2'), ('ab1', 'dc3'), ('ab1', 'xx4'), (' aa2', 'ab1'), ('aa2', 'aa2'), ('aa2', 'dc3'), ('aa2', 'xx4'), ('dc3', 'ab1'), (' dc3', 'aa2'), ('dc3', 'dc3'), ('dc3', 'xx4'), ('xx4', 'ab1'), ('ab1', 'aa2'), (' ab1', 'dc3'), (' xx4','xx4')。我这样做的单个文件至少少于一百万个代码。

import os, import pandas as pd, import itertools
os.chdir('C:/Users/Rashid/Desktop/Try')


df = pd.read_excel("code1.xlsx", header=None, index_col=False, dtype=str)
df.columns = ['A']

i = 0
lines = []
for item in itertools.product(df['A'], repeat=4):
    lines.append(item)

    if len(lines) > 50000:
        with open(f'4ta.{i}.txt', 'a') as f:
            f.write(''.join(str(lines)))
        lines = []
        i += 1

标签: pythonpandascombinations

解决方案


如果您的所有数据都在df“A”列中,则:

for i, j in permutations(df["A"], r=2):
    print(i, j)

将产生所有可能的排列。


推荐阅读