導航:首頁 > 編程語言 > python多個list元素組合

python多個list元素組合

發布時間:2022-12-29 05:31:30

① 如何排列組合合並python里兩個list的元素

可以使用for循環嵌套,也可以使用proct幫你生成。
l1=[1,2,3,4]
l2=['a','b','c','d']
fori,jinproct(l1,l2):
print(i,j)
1a
1b
1c
1d
2a
2b
2c
2d
3a
3b
3c
3d
4a
4b
4c
4d

② python多個列表的的元素組合成一個列表

result=[]
foriinrange(len(a)):
result.append([a[i],b[i],c[i],d[i])
print(result)

寫的有點low,不過應該能跑

③ python怎麼生成list的所有元素的組合

生成排列可以用proct:
from itertools import proct
l = [1, 2, 3]
print list(proct(l, l))
print list(proct(l, repeat=4))

組合的話可以用combinations:
from itertools import combinations
print list(combinations([1,2,3,4,5], 3))

下面是我以為沒有combinations然後自己寫的,沒有itertools的python(2.6以下)可供參考。
import

def combine(l, n):
answers = []
one = [0] * n
def next_c(li = 0, ni = 0):
if ni == n:
answers.append(.(one))
return
for lj in xrange(li, len(l)):
one[ni] = l[lj]
next_c(lj + 1, ni + 1)
next_c()
return answers

print combine([1, 2, 3, 4, 5], 3)

輸出:
[[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]

④ 在Python的IDLE中如何讓兩個list中特定元素組合在一起

這不很簡單嗎直接用索引列印不就可以了,print(list2[0]+list1[1]+','+list2[2]+list1[2])

⑤ 如何排列組合合並Python里兩個list的元素

排列組合合並Python里兩個list的元素
import itertools
a,b=[1,2,3],[4,5,6]
print(list(itertools.proct(a,b)))

⑥ 在python中如何把多個元素放在一個列表裡

⑦ python怎麼簡單的生成多個list的元素組合

生成排列可以用proct:

1 from itertools import proct
2 l = [1, 2, 3]
3 print list(proct(l, l))
4 print list(proct(l, repeat=4))

組合的話可以用combinations:

1 from itertools import combinations
2 print list(combinations([1,2,3,4,5], 3))

想更好的學習python請關注微信公眾號「Python基礎教程」!

⑧ 如何排列組合合並Python里兩個list的元素

error 是 NameError, createDeck 說是not defined.
報這個錯誤的話,是你寫函數名字,寫錯了,檢查拼寫錯誤。
但你提供的代碼,沒有看到這樣的錯誤。
Python 報錯信息太友好了,提示在哪行,有啥錯誤,要習慣看報錯信息調試代碼。

閱讀全文

與python多個list元素組合相關的資料

熱點內容
怎麼把已安裝的app放到桌面 瀏覽:942
如何查看蘋果手機app是否取消訂閱 瀏覽:769
u盤加密之後手機可以打開嗎 瀏覽:42
單片機串口發射怎麼回事 瀏覽:474
程序員假裝自己很忙 瀏覽:798
程序員能力關鍵詞 瀏覽:615
plc編程高級視頻教程 瀏覽:613
java遞歸求n 瀏覽:87
python絕對路徑導入 瀏覽:131
nex5g加密 瀏覽:979
18的空島伺服器地址 瀏覽:90
程序員要學什麼硬體 瀏覽:668
股票漲跌源碼怎麼看 瀏覽:580
加密軟體做法 瀏覽:59
美國程序員有多少中國人 瀏覽:741
人民日報app里怎麼看新聞早班車 瀏覽:589
忘了app怎麼辦 瀏覽:533
如何用雲伺服器做雲平台 瀏覽:303
非箍筋加密區剪力 瀏覽:121
利聯科技伺服器卡怎麼辦 瀏覽:388