绕过列表理解中的非类型

发布于 2025-01-31 04:48:30 字数 270 浏览 1 评论 0原文

要在没有值x.competition.name

'competition': [x.competition.name if x.competition.name is not None else '-' for x in a]

情况

AttributeError: 'NoneType' object has no attribute 'name'

的 围绕这个问题?

To work around when there is no value x.competition.name, I tried to use is not None:

'competition': [x.competition.name if x.competition.name is not None else '-' for x in a]

But the error still keeps showing up:

AttributeError: 'NoneType' object has no attribute 'name'

How can I go about getting around this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

会发光的星星闪亮亮i 2025-02-07 04:48:30

听起来您需要测试x.competition

'竞赛':[x.competition.name如果x.com.petition不是没有其他' - >

It sounds like you need to test x.competition:

'competition': [x.competition.name if x.competition is not None else '-' for x in a]

我很坚强 2025-02-07 04:48:30

显然没有竞争,所以请

[x.competition.name if x.competition.name is not None else '-' for x in a]

使用

[x.competition.name if x.competition is not None else '-' for x in a]

Apparently competition is None, so please replace

[x.competition.name if x.competition.name is not None else '-' for x in a]

using

[x.competition.name if x.competition is not None else '-' for x in a]
衣神在巴黎 2025-02-07 04:48:30

错误消息说您尝试获得none.name。这意味着x.competition必须是,这就是为什么您无法从中获得name属性的原因。相反,尝试使您的条件x.com.petition不是无

The error message is saying that you tried to get None.name. That means x.competition must be None, which is why you can't get a name attribute from it. Instead, try making your condition x.competition is not None.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文