最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)求一個(gè)集合所有子集的示例

 更新時(shí)間:2018年05月04日 15:11:57   作者:tszw1007  
今天小編就為大家分享一篇Python 實(shí)現(xiàn)求一個(gè)集合所有子集的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

方法一:回歸實(shí)現(xiàn)

def PowerSetsRecursive(items):
  """Use recursive call to return all subsets of items, include empty set"""
  
  if len(items) == 0:
    #if the lsit is empty, return the empty list
    return [[]]
  
  subsets = []
  first_elt = items[0] #first element
  rest_list = items[1:]
  
  #Strategy:Get all subsets of rest_list; for each of those subsets, a full subset list
  #will contain both the original subset as well as a version of the sebset that contains the first_elt
  
  for partial_sebset in PowerSetsRecursive(rest_list):
    subsets.append(partial_sebset)
    next_subset = partial_sebset[:] +[first_elt]
    subsets.append(next_subset)
  return subsets

def PowerSetsRecursive2(items):
  # the power set of the empty set has one element, the empty set
  result = [[]]
  for x in items:
    result.extend([subset + [x] for subset in result])
  return result 

方法二:二進(jìn)制法

def PowerSetsBinary(items): 
  #generate all combination of N items 
  N = len(items) 
  #enumerate the 2**N possible combinations 
  for i in range(2**N): 
    combo = [] 
    for j in range(N): 
      #test jth bit of integer i 
      if(i >> j ) % 2 == 1: 
        combo.append(items[j]) 
    yield combo 

以上這篇Python實(shí)現(xiàn)求一個(gè)集合所有子集的示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

南溪县| 蒙自县| 萨迦县| 桦甸市| 汨罗市| 内乡县| 濮阳县| 开平市| 汉中市| 乐至县| 余江县| 侯马市| 周至县| 梅州市| 大余县| 霍林郭勒市| 屯昌县| 海阳市| 满城县| 文昌市| 邳州市| 托克逊县| 平乐县| 阜南县| 定结县| 靖江市| 白水县| 静安区| 精河县| 东海县| 巴塘县| 新疆| 五台县| 四会市| 高邑县| 天台县| 株洲市| 奇台县| 葫芦岛市| 中牟县| 永新县|