繁体   English   中英

查找 python 中 4 个数字的所有长度为 9 的组合

[英]Find all combinations of length 9 for of 4 numbers in python

我有这四个数字 0、1、2,3,我想找到长度为 9 的所有可能组合。让我们先对长度 2 进行操作。 我们有:

  • 0 0
  • 0 1
  • 0 2
  • 0 3
  • 1 0
  • 1 2
  • 1 3
  • 1 4
  • ...

所以我想得到长度为 9 的这些:

  • 0 0 0 0 0 0 0 0 0
  • 0 0 0 0 0 0 0 0 1
  • 0 0 0 0 0 0 0 1 1
  • 0 0 0 0 0 0 1 1 1
  • ...

您可以使用itertools.product构造笛卡尔积。 相关表格是这样的:

itertools.product(range(4), repeat=9)

它是一个生成器,因此您必须对其进行迭代才能看到它:

import itertools
for p in itertools.product(range(4), repeat=9):
    print(p)

纯算法(可用于每个字符序列):

def combine(chars, length, s=""):
    for i in chars:
        s += str(i)
        combine(chars, length-1, s) if length-1 > 0 else result.append(s)
        s = s[:-1]


result = []
combine(["0", "1", "2", "3"], 4)
print(result)

结果:

['0000', '0001', '0002', '0003', '0010', '0011', '0012', '0013', '0020', '0021', '0022', '0023', '0030', '0031', '0032', '0033', '0100', '0101', '0102', '0103', '0110', '0111', '0112',  '0113', '0120', '0121', '0122', '0123', '0130', '0131', '0132', '0133', '0200', '0201', '0202', '0203', '0210', '0211', '0212', '0213', '0220', '0221', '0222', '0223', '0230', '0231',  '0232', '0233', '0300', '0301', '0302', '0303', '0310', '0311', '0312', '0313', '0320', '0321', '0322', '0323', '0330', '0331', '0332', '0333', '1000', '1001', '1002', '1003', '1010',  '1011', '1012', '1013', '1020', '1021', '1022', '1023', '1030', '1031', '1032', '1033', '1100', '1101', '1102', '1103', '1110', '1111', '1112', '1113', '1120', '1121', '1122', '1123',  '1130', '1131', '1132', '1133', '1200', '1201', '1202', '1203', '1210', '1211', '1212', '1213', '1220', '1221', '1222', '1223', '1230', '1231', '1232', '1233', '1300', '1301', '1302',  '1303', '1310', '1311', '1312', '1313', '1320', '1321', '1322', '1323', '1330', '1331', '1332', '1333', '2000', '2001', '2002', '2003', '2010', '2011', '2012', '2013', '2020', '2021',  '2022', '2023', '2030', '2031', '2032', '2033', '2100', '2101', '2102', '2103', '2110', '2111', '2112', '2113', '2120', '2121', '2122', '2123', '2130', '2131', '2132', '2133', '2200',  '2201', '2202', '2203', '2210', '2211', '2212', '2213', '2220', '2221', '2222', '2223', '2230', '2231', '2232', '2233', '2300', '2301', '2302', '2303', '2310', '2311', '2312', '2313',  '2320', '2321', '2322', '2323', '2330', '2331', '2332', '2333', '3000', '3001', '3002', '3003', '3010', '3011', '3012', '3013', '3020', '3021', '3022', '3023', '3030', '3031', '3032',  '3033', '3100', '3101', '3102', '3103', '3110', '3111', '3112', '3113', '3120', '3121', '3122', '3123', '3130', '3131', '3132', '3133', '3200', '3201', '3202', '3203', '3210', '3211',  '3212', '3213', '3220', '3221', '3222', '3223', '3230', '3231', '3232', '3233', '3300', '3301', '3302', '3303', '3310', '3311', '3312', '3313', '3320', '3321', '3322', '3323', '3330',  '3331', '3332', '3333']

这是一个简单的递归解决方案,您可以查看:

nums = [0, 1, 2, 3]
length = 4 # for example

def gen_all_seq(nums, length):
    all_seq = []
    def _recurse(l, nums):
        if len(l) == length:
            all_seq.append(l.copy())
            return
        for num in nums:
            l.append(num)
            _recurse(l, nums)
            l.pop()
        return
    _recurse([], nums)
    return all_seq

print(len(gen_all_seq(nums, length))) # prints 256

使用itertools.combinations_with_replacement

from itertools import combinations_with_replacement

a = combinations_with_replacement([0,1,2,3], 9)

print(list(a))

使用itertools模块的产品function。

import itertools

x = [0,1, 2, 3]
a = [p for p in itertools.product(x, repeat=9)]
print(a)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM