在 python 中,“if-else 和 for"如何处理?在字典理解工作中 [英] In python how does "if-else and for" in a dictionary comprehension work

查看:37
本文介绍了在 python 中,“if-else 和 for"如何处理?在字典理解工作中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码行感到困惑:

I am confused with the following line of code:

data = {n.attributes['xid']: float(n.content) if n.content else np.nan for n in graph.by_tag('value') }

字典理解由if-elsefor 循环组成.谁能解释一下代码是如何工作的?

The dictionary comprehension consists of if-else and for loop. Can anyone explain me how the code works?

推荐答案

翻译有帮助吗?

data = {}
for n in graph.by_tag('value'):
    if n.content:
        data[n.attributes['xid']] = float(n.content)
    else:
        data[n.attributes['xid']] = np.nan

这篇关于在 python 中,“if-else 和 for"如何处理?在字典理解工作中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆