① 如何排列组合合并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 报错信息太友好了,提示在哪行,有啥错误,要习惯看报错信息调试代码。