Python:列出列表中所有元素的组合可能


Python:列出列表中所有元素的组合可能

 

from itertools import combinations

def combine(temp_list, n):
'''根据n获得列表中的所有可能组合(n个元素为一组)'''
temp_list2 = []
for c in combinations(temp_list, n):
temp_list2.append(c)
return temp_list2

list1 = [1,2,3,4,5]
end_list = []
for i in range(len(list1)):
if combine(list1, i)!=[()]:
end_list.extend(combine(list1, i))
print(len(end_list))
for i in set(end_list):
print(i)

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM