求一个集合的所有子集 Python实现
生活随笔
收集整理的這篇文章主要介紹了
求一个集合的所有子集 Python实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
求一個集合的所有子集 Python實現
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Jun 23 16:59:07 2018@author: luogan """def PowerSetsBinary(items): #generate all combination of N items N = len(items) #enumerate the 2**N possible combinations set_all=[]for i in range(2**N):#print('i=',i)#print('__'*10)combo = [] for j in range(N): #print('j=',j)#test jth bit of integer i if(i >> j ) % 2 == 1: print('i=',i,'j=',j)combo.append(items[j]) #print(combo)#yield combo #print(combo)set_all.append(combo)return set_alla=list(range(3))out= PowerSetsBinary(a)print(out [[], [0], [1], [0, 1], [2], [0, 2], [1, 2], [0, 1, 2]]posted on 2018-06-23 17:12 luoganttcc 閱讀(...) 評論(...) 編輯 收藏
總結
以上是生活随笔為你收集整理的求一个集合的所有子集 Python实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 位运算符号
- 下一篇: python 实现函数的递归