zoukankan      html  css  js  c++  java
  • 求一个集合的所有真子集 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 """

    探究未知是最大乐趣
  • 相关阅读:
    Django中前端界面实现级联查询
    二叉树遍历规则
    计算机组成原理面试总结
    用python介绍4种常用的单链表翻转的方法
    跨域资源共享(CORS)
    python中单例模式的四种实现方式
    算法和数据结构
    手写配置文件实现两套配置文件的切换
    CentOS 7 安装教程
    配置管理系统
  • 原文地址:https://www.cnblogs.com/Mufasa/p/7647497.html
Copyright © 2011-2022 走看看