三种推导式:list,dict,set

1.列表推导式:

规范式:variable = [out_exp for out_exp in input_list if out_exp == 2]

multiples = [i for i in range(30) if i % 3 is 0]
print(multiples)

2.字典推导式:

mcase = {'a': 10, 'b': 34, 'A': 7, 'Z': 3}
mcase_frequency = {
k.lower(): mcase.get(k.lower(), 0) + mcase.get(k.upper(), 0)
for k in mcase.keys()}
mcase_frequency

mcase = {'a': 10, 'b': 34, 'A': 7, 'Z': 3}
{v: k for k,v in mcase.items()}

3.集合推导式:

squared = {x**2 for x in [1, 1, 2]}
print(squared)

 

你可能感兴趣的:(python学习)