一、*符号
1.解包所有可迭代对象,比如list、tuple、string
1 2 3 4
| num_list = [1, 2, 3, 4, 5] num_list_2 = [6, 7, 8, 9, 10] new_list = [*num_list, *num_list_2] print(new_list)
|
2.剩余赋值
1 2
| first, *middle, last = 'ma' print(first, middle, last)
|
3.打包
1 2
| *names, = 'Michael', 'John', 'Nancy' print(names)
|
4.函数传参
1 2 3 4
| def names_tuple(*args): return args names_tuple('Michael', 'John', 'Nancy')
|
二、**符号
1.函数传参
1 2 3 4 5 6
| def names_dict(**kwargs): return kwargs names_dict(Jane = 'Doe')
names_dict(Jane = 'Doe', John = 'Smith')
|
2.合并字典
1 2 3 4
| num_dict = {'a': 1, 'b': 2, 'c': 3} num_dict_2 = {'a': 4, 'e': 5, 'f': 6} new_dict = {**num_dict, **num_dict_2} print(new_dict)
|
参考文章:https://towardsdatascience.com/unpacking-operators-in-python-306ae44cd480