求一个集合的所有真子集 Python

给定一个集合,元素均为正整数且不重复,求该集合的所有子集

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Tue Oct 10 09:04:53 2017
 4 
 5 @author: nvidia
 6 """
 7 
 8 #!/usr/bin/python
 9 #coding=utf-8
10 #求一个集合的所有真子集
11 def getRealSubSet(fromList,toList):
12   if(len(fromList) <= 1):
13   return
14   for id in range(len(fromList)):
15   arr = [i for i in fromList if i != fromList[id]]
16   getRealSubSet(arr,toList)
17   #print arr
18   if(toList.count(arr) == 0):
19   toList.append(arr)
20 
21 li = []
22 getRealSubSet([3,4,5],li)
23 li.sort()
24 print li
25 
26 #以上为第二题答案
27 #after charging ,it was all right!
28 
29 """
30 the down words are the result!
31 Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
32 Type "copyright", "credits" or "license" for more information.
33 
34 IPython 2.4.1 -- An enhanced Interactive Python.
35 ? -> Introduction and overview of IPython's features.
36 %quickref -> Quick reference.
37 help -> Python's own help system.
38 object? -> Details about 'object', use 'object??' for extra details.
39 %guiref -> A brief reference about the graphical user interface.
40 
41 In [1]: runfile('/home/nvidia/Documents/work/20171010.py', wdir='/home/nvidia/Documents/work')
42 [[3], [3, 4], [3, 5], [4], [4, 5], [5]]
43 
44 In [2]: 
45 
46 """

posted on 2017-10-10 19:51  周健康  阅读(1780)  评论(0编辑  收藏  举报

导航