一:zip函数
print(list(zip((‘a‘,‘b‘,‘c‘),(‘1‘,‘2‘,‘3‘)))) print(list(zip((‘a‘,‘b‘),(‘1‘,‘2‘,‘3‘)))) print(list(zip((‘a‘,‘b‘,‘c‘),(‘1‘,‘2‘)))) p={‘name‘:‘jake‘,‘age‘:‘20‘} print(list(zip(p.keys(),p.values()))) print(list(zip([‘a‘,‘b‘],‘sdfghh‘)))
输出为:[(‘a‘, ‘1‘), (‘b‘, ‘2‘), (‘c‘, ‘3‘)]
[(‘a‘, ‘1‘), (‘b‘, ‘2‘)]
[(‘a‘, ‘1‘), (‘b‘, ‘2‘)]
[(‘name‘, ‘jake‘), (‘age‘, ‘20‘)]
[(‘a‘, ‘s‘), (‘b‘, ‘d‘)]
二:max函数(min同理)
#例1 List=[1,2,3,4,5,100] print(max(List)) print(min(List)) age_dic={‘age1‘:18,‘age2‘:20,‘age3‘:16,‘age122222‘:56,‘bs‘:16} print(max(age_dic.values())) #默认比较的是字典的key print(max(age_dic)) print(max(age_dic.keys())) print(list(max(zip(age_dic.values(),age_dic.keys())))) #例2 #max函数传入的参数必须是可迭代类型,且不同类型之间不能比较 l1=[(5,‘a‘),(4,‘e‘),(2,‘f‘)] l2=[‘a1‘,‘b2‘,‘c3‘] l3=[‘a10‘,‘a2‘,‘a‘] print(list(max(l1))) print(list(max(l2))) print(list(max(l3))) #例3 human=[ {‘name‘:‘a‘,‘age‘:10}, {‘name‘:‘b‘,‘age‘:19}, {‘name‘:‘c‘,‘age‘:16} ] #max(human)会报错,因为字典是无序的,max无法按对应位置进行比较 print(max(human,key=lambda dic:dic[‘age‘]))
输出为:100
1
56
bs
bs
[56, ‘age122222‘]
[5, ‘a‘]
[‘c‘, ‘3‘]
[‘a‘, ‘2‘]
{‘name‘: ‘b‘, ‘age‘: 19}
原文地址:https://www.cnblogs.com/wangdamao/p/10443413.html
时间: 2024-11-07 22:11:57