Advanced Functions - Dictionary of formula

A dictionary Dian formula

print({i: i**2 for i in range(10)})

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
{} Acquired personal summary format the key: value + for iterative loop elements (ps: key, value or a fixed value, either through the elements associated with operation)

Dian two zip () method

keys = ['name', 'age', 'gender']
values = ['nash', 19, 'male']

res = zip(keys, values)
print(F"zip(keys,values): {zip(keys,values)}")

info_dict = {k: v for k, v in res}
print(f"info_dict: {info_dict}")

zip(keys,values): <zip object at 0x11074c088> info_dict: {'name': 'nash', 'age': 19, 'sex': 'male'}
Generated by decompressing a dictionary function

info_dict = {'name': 'nash', 'age': 18, 'gender': 'male'}
print(f"info_dict.keys(): {info_dict.keys()}")
print(f"info_dict.values(): {info_dict.values()}")

res = zip(info_dict.keys(), info_dict.values())
print(F"zip(keys,values): {zip(info_dict.keys(),info_dict.values())}")

info_dict = {k: v for k, v in res}
print(f"info_dict: {info_dict}")

info_dict.keys(): dict_keys(['name', 'age', 'gender']) info_dict.values(): dict_values(['nash', 18, 'male']) zip(keys,values): <zip object at 0x1105cefc8> info_dict: {'name': 'nash', 'age': 18, 'gender': 'male'}

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374763.html
Recommended