① 如何排列組合合並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中如何把多個元素放在一個列表裡
打開pycharm開發工具,新建python文件並定義列表變數a1,進行賦值
⑦ 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 報錯信息太友好了,提示在哪行,有啥錯誤,要習慣看報錯信息調試代碼。