肉饼博客

Talk is cheap. Show me the code.

0%

星号操作符

一、*符号

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) # [1,2,3,4,5,6,7,8,9,10]

2.剩余赋值

1
2
first, *middle, last = 'ma'
print(first, middle, last) # m [] a

3.打包

1
2
*names, = 'Michael', 'John', 'Nancy'
print(names) # ['Michael', 'John', 'Nancy']

4.函数传参

1
2
3
4
def names_tuple(*args):
return args
names_tuple('Michael', 'John', 'Nancy')
# ('Michael', 'John', 'Nancy')

二、**符号

1.函数传参

1
2
3
4
5
6
def names_dict(**kwargs):
return kwargs
names_dict(Jane = 'Doe')
# {'Jane': 'Doe'}
names_dict(Jane = 'Doe', John = 'Smith')
# {'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) # {'a': 4, 'b': 2, 'c': 3, 'e': 5, 'f': 6}

参考文章:https://towardsdatascience.com/unpacking-operators-in-python-306ae44cd480