# 创建一个目录。若该目录存在,则删除该目录并重新创建
import shutil
def make_empty_dir(dir_path):
try:
shutil.rmtree(dir_path)
except:
pass
os.mkdir(dir_path)
# 将(中文)字典保存为json文件
import json
d = {'浙江':['杭州','宁波','温州'],
'江苏':['南京','苏州','无锡'],
'福建':['福州','厦门','泉州']}
with open('d:/sample.json', 'w', encoding='utf-8-sig') as f:
json_data = json.dumps(d, indent=4, ensure_ascii=False)
f.write(json_data)
# 求集合(实际是列表)所有子集
def PowerSetsRecursive(items):
"""Use recursive call to return all subsets of items, include empty set"""
if len(items) == 0:
return [[]]
subsets = []
first_elt = items[0] #first element
rest_list = items[1:]
for partial_sebset in PowerSetsRecursive(rest_list):
subsets.append(partial_sebset)
next_subset = partial_sebset[:] +[first_elt]
subsets.append(next_subset)
return subsets