一、集合推导式:
{}类似于列表推导式,在列表推导式的基础上添加了一个去除重复项、
示例:
1 """
2 集合推导式:
3 {}类似于列表推导式,在列表推导式的基础上添加了一个去除重复项
4 """
5 list1 = [1,2,3,4,5,6,7,8,6,5,4,3,5,0]
6 set1 = {x for x in list1}
7 print(set1)
结果:{0, 1, 2, 3, 4, 5, 6, 7, 8}
二、字典推导式
1 dict1 = {'a':"A","b":"B","c":"C","d":"C"}
2 new_dict = {value:key for key,value in dict1.items()}
3 print(new_dict)
结果:{'C': 'd', 'A': 'a', 'B': 'b'}