cities = { 'shanghai':{'country':'china','population':10000,'fact':'good'}, 'lendon':{'country':'england','population':2348,'fact':'nice'}, 'new york':{'country':'american','population':8650,'fact':'rich'}, } for city,characts in cities.items(): prin
一.数据筛选: 处理方式: 1.filter函数在py3,返回的是个生成式. from random import randint data = [randint(-100,100) for i in range(10)] data2 = [34, -59, -13, 96, -78, 38, 89, -96, -79, 98] info = filter(lambda x:x>0,data2) for i in info: print(i) 2.列表解析 from random import
字典的元素是成键值对出现的,直接对字典使用sorted() 排序,它是根据字典的键的ASCII编码顺序进行排序,要想让字典根据值的大小来排序,可以有两种方法来实现: 一.利用zip函数将字典数据转化为元组再用sorted() 排序 from random import randint # 用随机函数生成待排序的字典数据 my_dict = {x: randint(60, 100) for x in 'abcxyzgkj'} # 将字典数据转化为元组,把字典的值作为元组的第0项,键作为元组的第1项