內置函數

sorted

語法:sorted(iterable,key=function,reverse)reverse -- 排序規則,reverse = True 降序 , reverse = False 升序(默認)。html

lst = [
    {'name':"A","age":48},
    {"name":"B",'age':38},
    {"name":"C","age":39},
    {"name":"D","age":32},
    {"name":"E","age":28}
    ]

ll = sorted(lst, key=lambda el: len(el['name']), reverse=True)
print(ll)

filter

函數用於過濾序列,過濾掉不符合條件的元素,返回由符合條件元素組成的新列表。python

該接收兩個參數,第一個爲函數,第二個爲序列,序列的每一個元素做爲參數傳遞給函數進行判,而後返回 True 或 False,最後將返回 True 的元素放到新列表中。函數

lst = [
    {"name":"A", "score":48},
    {"name":"B", "score":39},
    {"name":"C","score":97},
    {"name":"D","score":90}
]

f = filter(lambda el: el['score'] < 60 , lst)

print(list(f))

map

map() 函數語法:map(function, iterable, ...)ui

特色:spa

分而治之
map(func1, map(func2, map(func3 , lst)))code

水桶效應,與 zip()相似htm

lst1 = [1, 3, 5, 7]
lst2 = [2, 4, 6, 8, 10]
m = map(lambda x, y, z: x + y+ z, lst1, lst2, [5,1,2,3,6])
print(list(m))
運行結果:[8, 8, 13, 18]

其餘內置函數移步:blog

菜鳥傳送門排序

相關文章
相關標籤/搜索