【发布时间】:2017-12-05 04:58:47
【问题描述】:
如何在 python 中获得长度为 n 的列表的所有可能组合我已经为字符串编写了一个代码,但是列表我无法做到,谁能帮助我。
def combo(w, l):
lst = []
for i in range(len(w)):
if l == 1:
lst.append(w[i])
for c in combo(w[i+1:], l-1):
lst.append(w[i] + c)
return lst
comb=map(list,combo('12345',3))
上面的代码是为了得到它给出正确输出的字符串组合:
['12', '13', '14', '15', '23', '24', '25', '34', '35', '45']
【问题讨论】:
标签: python python-3.x python-2.7