是否可以在列表理解中使用“ else”?[重复]
发布于 2021-01-29 14:58:04
这个问题已经在这里有了答案 :
如果/否则列表理解
(11个答案)
去年关闭。
这是我试图变成列表理解的代码:
table = ''
for index in xrange(256):
if index in ords_to_keep:
table += chr(index)
else:
table += replace_with
有没有一种方法可以将else语句添加到此理解中?
table = ''.join(chr(index) for index in xrange(15) if index in ords_to_keep)
关注者
0
被浏览
76
1 个回答