'''
python中常見的內置函數
'''
"""
max,找出工資最高的那個人
"""
salaries={
'egon':3000,
'alex':100000000,
'wu':10000,
'yuan':2000
}
res = max(salaries, key=lambda x: salaries[x])
print(res) # alex
"""
sorted,按字典中的value進行排序
"""
res2 = sorted(salaries, key=lambda x: salaries[x],reverse=True)
print(res2) # ['alex', 'wu', 'egon', 'yuan']
"""
map
map(function, iterable, ...)
Map applies a function to all the items in an input_list, and return a map object,like generator
"""
l=[1,2,3]
a = map(lambda x: x**2, l)
print(list(a)) # [1, 4, 9]
"""
filter
filter(function, iterable)
filter creates a list of elements for which a function returns true
"""
salaries={
'egon':3000,
'alex':100000000,
'wu':10000,
'yuan':2000
}
#通過filter函數輸出工資大于10000的人名
c = filter(lambda m: salaries[m] > 100000,salaries)
print(list(c)) # ['alex']
"""
reduce
reduce(function, iterable)
Reduce is a really useful function for performing some computation on a list and returning the result.
"""
from functools import reduce
l=[1, 2, 3]
b = reduce(lambda x,y: x+y,l)
print(b) # 6
python常見內置函數
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
推薦閱讀更多精彩內容
- Pre-conditions: Import all PyQt libs into Python project(...
- #####1.局部變量&全局變量局部變量是在函數內部定義的,全局變量是在函數的外部定義的,在函數的內部可以訪問全局...