itertools.permutations(iterable, r = None)
例如由数字 456 的各个位重新排列能组成哪些数字:
from itertools import *
n = 456
n = list(str(n))
all = list(permutations(n))
# Int[0]:print(permutations(n))
# Out[0]:
print(all)
res = set()
for i in all:
num = int("".join(i))
res.add(num)
print(res)
Out[1]:[('4', '5', '6'), ('4', '6', '5'), ('5', '4', '6'), ('5', '6', '4'), ('6', '4', '5'), ('6', '5', '4')]
Out[2]:{546, 645, 456, 654, 465, 564}
当要获取的一个key未在字典中时,一般会报错,就可能它给指定一个默认值
In [1]: data = {"name":"xm", "age":18}
In [2]: gender = data.get("gender", "男")
In [3]: gender
Out[3]: '男'
我们也可以将不存在的值添加进字典,并指定一个默认值
In [4]: data
Out[4]: {'name': 'xm', 'age': 18}
In [5]: gender = data.setdefault("gender","男")
In [6]: gender
Out[6]: '男'
In [7]: data
Out[7]: {'name': 'xm', 'age': 18, 'gender': '男'}
In [10]: data1 = {"name":"zhangsan","age":30}
In [11]: data2 = {"gender":"男","height":180}
In [12]: data={**data1,**data2}
In [13]: data
Out[13]: {'name': 'zhangsan', 'age': 30, 'gender': '男', 'height': 180}
合并方式还有几种,下次记得了再更新
后面更一些元组列表字典的东西