国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)從字典中提取子集的方法示例

2020-01-04 15:24:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Python從字典中提取子集的方法。分享給大家供大家參考,具體如下:

問(wèn)題:想創(chuàng)建一個(gè)字典,其本身是另一個(gè)字典的子集

解決方案:利用字典推導(dǎo)式(dictionary comprehension)可輕松解決

# example of extracting a subset from a dictionaryfrom pprint import pprintprices = { 'ACME': 45.23, 'AAPL': 612.78, 'IBM': 205.55, 'HPQ': 37.20, 'FB': 10.75}# Make a dictionary of all prices over 200p1 = { key:value for key, value in prices.items() if value > 200 }print("All prices over 200")pprint(p1)# Make a dictionary of tech stockstech_names = { 'AAPL', 'IBM', 'HPQ', 'MSFT' }p2 = { key:value for key,value in prices.items() if key in tech_names }print("All techs")print(p2)

運(yùn)行結(jié)果:

All prices over 200{'AAPL': 612.78, 'IBM': 205.55}All techs{'AAPL': 612.78, 'HPQ': 37.2, 'IBM': 205.55}

字典推導(dǎo)式的方案清晰且運(yùn)行起來(lái)很快。

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长沙县| 临泉县| 嘉峪关市| 高平市| 商南县| 沧州市| 亳州市| 竹山县| 乌拉特中旗| 盐池县| 桃园市| 杨浦区| 福鼎市| 宝丰县| 桑日县| 武定县| 从江县| 临桂县| 广元市| 平遥县| 汉阴县| 洪雅县| 重庆市| 丹巴县| 敦化市| 辉南县| 隆安县| 武功县| 壶关县| 九江县| 和平县| 庆阳市| 巴彦县| 达拉特旗| 吴桥县| 额济纳旗| 石阡县| 收藏| 湾仔区| 昌邑市| 波密县|