如何在字典理解中使用 if/else?
Python 2.7+ 中是否存在一种方法来制作类似以下内容的方法?
{ something_if_true if condition else something_if_false for key, value in dict_.items() }
我知道你可以用“如果”来做任何事情:
{ something_if_true for key, value in dict_.items() if condition}
Does there exist a way in Python 2.7+ to make something like the following?
{ something_if_true if condition else something_if_false for key, value in dict_.items() }
I know you can make anything with just 'if':
{ something_if_true for key, value in dict_.items() if condition}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您已经明白了:
A if test else B
是一个有效的 Python 表达式。如图所示,字典理解的唯一问题是字典理解中表达式的位置必须有两个表达式,并用冒号分隔:最后的
if
子句充当过滤器,这与具有条件表达式。You've already got it:
A if test else B
is a valid Python expression. The only problem with your dict comprehension as shown is that the place for an expression in a dict comprehension must have two expressions, separated by a colon:The final
if
clause acts as a filter, which is different from having the conditional expression.@Marcin 的答案涵盖了所有内容,但为了以防万一有人想查看实际示例,我在下面添加两个:
比方说您有以下集合字典
,并且想要创建一个新字典,其键指示字符串
'a'
是否包含在值中,您可以使用which 产生
现在让我们假设您有两个像这样的字典
,并且您想要用
d2
的键替换 d1 中的键,如果相应的值相同,您可以这样做
@Marcin's answer covers it all, but just in case someone wants to see an actual example, I add two below:
Let's say you have the following dictionary of sets
and you want to create a new dictionary whose keys indicate whether the string
'a'
is contained in the values or not, you can usewhich yields
Now let's suppose you have two dictionaries like this
and you want to replace the keys in
d1
by the keys ofd2
if there respective values are identical, you could dowhich gives
如果您有不同条件来评估键和值,@Marcin 的答案就是正确的选择。
如果键和值具有相同条件,那么最好在生成器表达式中构建(键,值)元组并将其输入到
dict 中()
:更容易阅读,并且每个键、值仅评估一次条件。
借用 @Cleb 集合字典的示例:
假设您只想在
keys
的value
中添加a
后缀,并且您想要value在这种情况下,
替换为集合的长度。否则,键值对应保持不变。In case you have different conditions to evaluate for keys and values, @Marcin's answer is the way to go.
If you have the same condition for keys and values, you're better off with building (key, value)-tuples in a generator-expression feeding into
dict()
:It's easier to read and the condition is only evaluated once per key, value.
Example with borrowing @Cleb's dictionary of sets:
Assume you want to suffix only
keys
witha
in itsvalue
and you want thevalue
replaced with the length of the set in such a case. Otherwise, the key-value pair should stay unchanged.还值得一提的是,If only 语句将 if 放在最后:
It also might be worth mentioning that If only statements put the if at the end:
在字典理解中使用 if/else 的另一个例子
我正在为自己的办公室工作开发数据输入桌面应用程序,这种数据输入应用程序通常从输入小部件获取所有条目并将其转储到字典中以供进一步使用诸如验证或编辑之类的处理,我们必须将选定的数据从文件返回到条目小部件等。
第一轮使用传统编码(8行):
第二轮我尝试使用字典理解,但循环仍然存在(6行):
最后,用一行字典理解语句(1行):
我使用python 3.8.3
Another example in using if/else in dictionary comprehension
I am working on data-entry desktop application for my own office work, and it is common for such data-entry application to get all entries from input widget and dump it into a dictionary for further processing like validation, or editing which we must return selected data from file back to entry widgets, etc.
The first round using traditional coding (8 lines):
Second round I tried to use dictionary comprehension but the loop still there (6 lines):
Finally, with a one-line dictionary comprehension statement (1 line):
I use python 3.8.3