小编Kha*_*fan的帖子

将 map() 与字典一起使用

我有一本字典。

prices = {'n': 99, 'a': 99, 'c': 147}

Run Code Online (Sandbox Code Playgroud)

使用map()我需要接收新的字典:

def formula(value):
    value = value -value * 0.05
    return value
Run Code Online (Sandbox Code Playgroud)
new_prices = dict(map(formula, prices.values()))
Run Code Online (Sandbox Code Playgroud)

但它不起作用

TypeError: cannot convert dictionary update sequence element #0 to a sequence
Run Code Online (Sandbox Code Playgroud)

使用以下方法解决我的代码map()

new_prices = {'n': 94.05, 'a': 94.05, 'c': 139.65}
Run Code Online (Sandbox Code Playgroud)

python dictionary list

2
推荐指数
1
解决办法
767
查看次数

标签 统计

dictionary ×1

list ×1

python ×1