我想不通遞歸,這裏是一個另類
#!/usr/bin/python
def combinations(L,length): #L is list [A,A+1, ... ,B]
length_L = len(L)
L_new=[]
for start in range(0,length_L):
if length > 1:
unit_self = retrieve_unit_self(L, start,length)
L_new.append(unit_self)
if start + length <= length_L:
unit = retrieve_unit(L, start,length)
L_new.append(unit)
return L_new;
def retrieve_unit_self(L,start,length):
unit=[]
for index in range(0,length):
unit.append(L[start])
return unit
def retrieve_unit(L,start,length):
unit=[]
for index in range(0,length):
unit.append(L[index+start])
return unit
def main():
L = ['a','b','c']
L_new = combinations(L,1)
print L_new
L_new = combinations(L,2)
print L_new
L_new = combinations(L,3)
print L_new
L_new = combinations(L,4)
print L_new
if __name__ == '__main__':
main()
結果:
./list.py
[['a'], ['b'], ['c']]
[['a', 'a'], ['a', 'b'], ['b', 'b'], ['b', 'c'], ['c', 'c']]
[['a', 'a', 'a'], ['a', 'b', 'c'], ['b', 'b', 'b'], ['c', 'c', 'c']]
[['a', 'a', 'a', 'a'], ['b', 'b', 'b', 'b'], ['c', 'c', 'c', 'c']]
你的意思是,如果你不能使用[with_replacement(https://開頭docs.python.org/2/library/itertools.html#itertools.combinations_with_replacement),對吧? – Brian
你的意思是說「它不工作」? – Psytho
我的意思是列表不斷的嵌套,正如有人已經指出的那樣。 – Acee