Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-itertools组合可伸缩性_Python_Combinations_Itertools - Fatal编程技术网

python-itertools组合可伸缩性

python-itertools组合可伸缩性,python,combinations,itertools,Python,Combinations,Itertools,我设法修改了在internet上找到的示例代码,以便在一个文件夹中找到一组文件的所有可能组合(2对) 如果我有一个包含以下文件的文件夹测试:file1、file2、file3、file4,并运行以下代码: import os, itertools, glob folder = "test" files = glob.glob(folder + "/*") counter = 0 for file1, file2 in itertools.combinations(files, 2): co

我设法修改了在internet上找到的示例代码,以便在一个文件夹中找到一组文件的所有可能组合(2对)

如果我有一个包含以下文件的文件夹测试:file1、file2、file3、file4,并运行以下代码:

import os, itertools, glob
folder = "test"

files = glob.glob(folder + "/*")
counter = 0
for file1, file2 in itertools.combinations(files, 2):
  counter = counter + 1
  output = file1 + " and " + file2
  print output, counter
我的输出是:

test/file1 and test/file2 1
test/file1 and test/file3 2
test/file1 and test/file4 3
test/file2 and test/file3 4
test/file2 and test/file4 5
test/file3 and test/file4 6
这非常适合列出2个文件的所有可能组,而无需重复。现在,由于我对for循环进行了硬编码,因此在将其扩展到“x”文件组的同时,仍然需要保持代码的简单性。即,我希望“x”是用户选择的,如果他选择3,脚本将显示以下输出:

test/file1 and test/file2 and test/file3 1
test/file1 and test/file2 and test/file4 2
test/file1 and test/file3 and test/file4 3
test/file2 and test/file3 and test/file4 4
整个想法并不是在stdout上实际显示输出,而是在子流程调用中将它们用作参数

有什么建议吗

x=3

for combination in itertools.combinations(files, x):
  counter = counter + 1
  output = " and ".join(combination)
  print output, counter
可以使用
sys.argv

可以使用
sys.argv