歡迎猛擊個人博客悟の跡【http://chihweihsu.com】更多精彩等着你


需求

給你一個條件,從已給字典中構造一個符合條件的新字典,為原字典的子集。

解決方案

使用字典推導式:

prices = {
    'ACME': 45.23,
    'AAPL': 612.78,
    'IBM': 205.55,
    'HPQ': 37.20,
    'FB': 10.75
}
# Make a dictionary of all prices over 200
p1 = {key: value for key, value in prices.items() if value > 200}
# Make a dictionary of tech stocks
tech_names = {'AAPL', 'IBM', 'HPQ', 'MSFT'}
p2 = {key: value for key, value in prices.items() if key in tech_names}

案例

# 需求:輸出phone_bill中每月不為'0.00'的項目
"phone_bill" : [
        {
            "bill_zengzhifei" : "0.00",
            "bill_qita" : "0.00",
            "bill_package" : "46.00",
            "bill_ext_sms" : "0.20",
            "bill_daishoufei" : "0.00",
            "bill_ext_data" : "0.00",
            "bill_ext_calls" : "0.00"
        },
        {
            "bill_zengzhifei" : "0.00",
            "bill_qita" : "0.00",
            "bill_package" : "46.00",
            "bill_ext_sms" : "0.60",
            "bill_daishoufei" : "0.00",
            "bill_ext_data" : "15.62",
            "bill_ext_calls" : "4.18"
        },
        {
            "bill_zengzhifei" : "0.00",
            "bill_qita" : "0.00",
            "bill_package" : "56.00",
            "bill_ext_sms" : "0.30",
            "bill_daishoufei" : "0.00",
            "bill_ext_data" : "9.36",
            "bill_ext_calls" : "7.03"
        },
        {
            "bill_zengzhifei" : "0.00",
            "bill_qita" : "0.00",
            "bill_package" : "46.00",
            "bill_ext_sms" : "0.30",
            "bill_daishoufei" : "0.00",
            "bill_ext_data" : "0.00",
            "bill_ext_calls" : "0.00"
        },
        {
            "bill_zengzhifei" : "0.00",
            "bill_qita" : "0.00",
            "bill_package" : "10.58",
            "bill_ext_sms" : "0.00",
            "bill_daishoufei" : "0.00",
            "bill_ext_data" : "0.00",
            "bill_ext_calls" : "0.00"
        }
    ]

# 處理邏輯
for cursor in xrange(0,5):
     phone_bill_tmp = {key: value[:-1] for key, value in phone_bill[cursor].items() if value not in ['','0.00']}
     phone_bill.append(phone_bill_tmp)

# 結果
"phone_bill" : [
        {
            "bill_package" : "46.0",
            "bill_ext_sms" : "0.2"
        },
        {
            "bill_package" : "46.0",
            "bill_ext_sms" : "0.6",
            "bill_ext_data" : "15.6",
            "bill_ext_calls" : "4.1"
        },
        {
            "bill_package" : "56.0",
            "bill_ext_sms" : "0.3",
            "bill_ext_data" : "9.3",
            "bill_ext_calls" : "7.0"
        },
        {
            "bill_package" : "46.0",
            "bill_ext_sms" : "0.3"
        },
        {
            "bill_package" : "10.5"
        }
    ]

思考

大多數情況下字典推導能做到的,通過創建一個元組序列然後把它傳給 dict() 函數也能實現。比如:

p1 = dict((key, value) for key, value in prices.items() if value > 200)

但是,字典推導方式表意更清晰,並且實際上也會運行的更快些 (在這個p1中,實際測試幾乎比 dcit() 函數方式快整整一倍)。

有時候完成同一件事會有多種方式。比如,p2程序也可以像這樣重寫:

# Make a dictionary of tech stocks
tech_names = { 'AAPL', 'IBM', 'HPQ', 'MSFT' }
p2 = { key:prices[key] for key in prices.keys() & tech_names }

但是,運行時間測試結果显示這種方案大概比第一種方案慢1.6倍。

所以,完成一個需求,方案並不是唯一的,也並沒有最完美的,只有更好的解決方案,如果對程序運行性能要求比較高的話,這就需要花點時間去做計時測試了。


博文轉載至我的個人博客網站–悟の跡,歡迎來訪。