我試圖寫一個函數,使得NCK從列表中蟒功能NCK選擇ķ從n個的列表中的元素在python
例如,從列表中對:
['a', 'b', 'c']
輸出應該是:
[['a','b'],['a','c'],['b','c']]
但是我沒有得到任何輸出
,這裏是我的嘗試:
def chose(elements, k):
output = []
for i in range(len(elements)):
if k == 1:
output.append(elements[i])
for c in chose(elements[i+1:], k-1):
output.append(elements[i])
output.append(c)
return output
print chose(['a', 'b', 'c'],2)
能不能請你告訴什麼是錯的功能
http://meta.stackoverflow.com/questions/254094/question-that-使用鹼性調試可待解決,。建議閱讀http://ericlippert.com/2014/03/05/how-to-debug-small-programs/。 –
另請參考:https://docs.python.org/2/library/itertools.html –