當前位置:首頁 » 編程語言 » 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))

熱點內容
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 瀏覽:166
動物園靈長類動物配置什麼植物 發布:2025-05-12 14:49:59 瀏覽:736
wifi密碼設置什麼好 發布:2025-05-12 14:49:17 瀏覽:148
三位數乘兩位數速演算法 發布:2025-05-12 13:05:48 瀏覽:398
暴風影音緩存在哪裡 發布:2025-05-12 12:42:03 瀏覽:544
access資料庫exe 發布:2025-05-12 12:39:04 瀏覽:632
五開的配置是什麼 發布:2025-05-12 12:36:37 瀏覽:365