unzip

table of Contents

unzip

If we give a list, we need to remove the one-time multiple values, we can not use the following way to achieve it?

name_list = ['nick', 'egon', 'jason']
x = name_list[0]
y = name_list[1]
z = name_list[2]
print(f'x:{x}, y:{y}, z:{z}')
x:nick, y:egon, z:jason

Speak true, the above method is really with who knows who, we can decompress try.

Decompression can be understood: the supermarket package is put together more merchandise, decompression is actually unpack multiple disposable goods out.

name_list = ['nick', 'egon', 'jason', ]
x, y, z = name_list
print(f'x:{x}, y:{y}, z:{z}')
x:nick, y:egon, z:jason

Sometimes we decompress values ​​may be we do not want, you can use an underscore, omnipotent underlined.

name_list = ['nick', 'egon', 'jason', 'tank']
x, y, z, a = name_list
x, _, z, _ = name_list  # _相当于告诉计算机不要了,不能以_开头和结尾

But also have a more Sao operation, can be felt but not explained.

name_list = ['nick', 'egon', 'jason', 'tank', 'kevin', 'jerry']
x, y, _, _, _, z = name_list
x, y, *_, z = name_list

Write a program not used to hold force, the key is to create value. python pursuit of simplicity, the code do not write too long. Therefore dictionary is also possible, but dictionaries decompression is key.

info = {'name': 'nick', 'age': 18}
x, y = info
print(x, y)
name age

Guess you like

Origin www.cnblogs.com/nickchen121/p/11069984.html
Recommended