包含If在阵列中使用For

Raf*_*fid 1 python

在Python中我有这样的说法:

blog_ids = [c.blog_id for c in connections]
Run Code Online (Sandbox Code Playgroud)

这基本上告诉Python在连接中创建所有博客ID的数组.不幸的是,如果connections对象有某些None类型,c.blog_id会导致异常.有没有解决这个问题的语法?我试过这个,但它不起作用:

blog_ids = [c.blog_id for c not None in connections]
Run Code Online (Sandbox Code Playgroud)

gru*_*czy 8

blog_ids = [c.blog_id for c in connections if c is not None]
Run Code Online (Sandbox Code Playgroud)