37,719
社区成员
发帖
与我相关
我的任务
分享
res_l=[]
def get_combine1(l,i):
res_l.append(l[i])
if len(res_l)>1:
for j in range(len(res_l)-1):
res_l.append(l[i]+res_l[j])
if i>0:
get_combine1(l,i-1)
l=["a","b","c","d"]
get_combine1(l,len(l)-1)
print(res_l)
输出:
D:\new\Python\excersise>python get_combine.py
['d', 'c', 'cd', 'b', 'bd', 'bc', 'bcd', 'a', 'ad', 'ac', 'acd', 'ab', 'abd', 'a
bc', 'abcd']
对输出顺序有要求,自己排下序。