当前位置:首页 » 编程语言 » python排列组合

python排列组合

发布时间: 2022-01-28 23:48:29

Ⅰ 在python中如何实现列表中元素的所有排列组合如输入为['1','2','3']和2输出为['

#!/usr/bin/python
#Two method for generate a list whose item is all possible permutation and combination come from every item of many list.

A = ['1', '2']
B = ['a', 'b', 'c']
C = ['A', 'B', 'C', 'D']

retList = []
for a in A:
for b in B:
for c in C:
retList.append((a,b,c))
print retList

print '*' * 40

def myfunc(*lists):
#list all possible composition from many list, each item is a tuple.
#Here lists is [list1, list2, list3], return a list of [(item1,item2,item3),...]

#len of result list and result list.
total = rece(lambda x, y: x * y, map(len, lists))
retList = []

#every item of result list.
for i in range(0, total):
step = total
tempItem = []
for l in lists:
step /= len(l)
tempItem.append(l[i/step % len(l)])
retList.append(tuple(tempItem))

return retList

print myfunc(A,B,C)

Ⅱ 怎么样让python计算任意六位数的排列组合

from itertools import proct
for i in proct(range(2),repeat=6):
print i

Ⅲ 如何用Python列出N个数字的所有排列组合

>> from itertools import combinations, permutations
>> permutations([1, 2, 3], 2)
<itertools.permutations at 0x7febfd880fc0>
# 可迭代对象
>> list(permutations([1, 2, 3], 2)) #排列
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
>> list(combinations([1, 2, 3], 2)) #组合
[(1, 2), (1, 3), (2, 3)]

Ⅳ Python取随机数排列组合

没懂你的意思,既然是排列组合,就是针对3个固定元素。
如果3个元素本身就是随机的,就不用排列了,下面的getplans函数也就没有意义,直接循环2-5行代码就好。

Ⅳ python 实现6个数的排列组合,每个数可选值为0和1

fromitertoolsimportproct
foriinproct(range(2),repeat=6):
printi

Ⅵ python新手问题:排列组合与输出最大数

python2.7语法

第一个问题:

Ⅶ 通过python实现0,1,2,3四个数排列组合可以组成多少个数

##缩进格式看图

import itertools

c=0

for e in itertools.permutations('0123',4):

print(''.join(e).lstrip('0'),end=',')

c+=1

print(' 共%d个'%(c))

热点内容
sql数据库远程备份 发布:2025-05-13 16:48:13 浏览:528
app什么情况下找不到服务器 发布:2025-05-12 15:46:25 浏览:714
php跳过if 发布:2025-05-12 15:34:29 浏览:467
不定时算法 发布:2025-05-12 15:30:16 浏览:131
c语言延时1ms程序 发布:2025-05-12 15:01:30 浏览:167
动物园灵长类动物配置什么植物 发布:2025-05-12 14:49:59 浏览:738
wifi密码设置什么好 发布:2025-05-12 14:49:17 浏览:150
三位数乘两位数速算法 发布:2025-05-12 13:05:48 浏览:399
暴风影音缓存在哪里 发布:2025-05-12 12:42:03 浏览:545
access数据库exe 发布:2025-05-12 12:39:04 浏览:632