开发者

Including If In Arrays Formed Using For

开发者 https://www.devze.com 2023-02-04 02:18 出处:网络
In Python I have this statement: blog_ids = [c.blog_id for c in connections] Which basically tells Python to create an array of all the blog IDs in the connections. Unfortunately, if the connection

In Python I have this statement:

blog_ids = [c.blog_id for c in connections]

Which basically tells Python to create an array of all the blog IDs in the connections. Unfortunately, if the connections object has some None types, c.blog_id would cause an exception. Is th开发者_运维百科ere any syntax to solve this problem? I tried this but it doesn't work:

blog_ids = [c.blog_id for c not None in connections]


blog_ids = [c.blog_id for c in connections if c is not None]


The thing here probably is to ask what kind of objects you have in your connections object. Are they either valid objects with blog_id attribute or None objects. Or is there possibility that among those objects there are also some other objects (in addition to None objects) without blog_id attribute.

0

精彩评论

暂无评论...
验证码 换一张
取 消