小编kon*_*lai的帖子

Python字典理解中的条件表达式

a = {"hello" : "world", "cat":"bat"}

# Trying to achieve this
# Form a new dictionary only with keys with "hello" and their values
b = {"hello" : "world"}

# This didn't work

b = dict( (key, value) if key == "hello" for (key, value) in a.items())
Run Code Online (Sandbox Code Playgroud)

关于如何在字典理解中包含条件表达式以决定是否应将键元组值包含在新字典中的任何建议

python dictionary-comprehension

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

dictionary-comprehension ×1

python ×1