这里是我试图变成一个列表理解的代码:是否可以在Python列表理解中使用'else'?
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)
注意的是Python 2.5,后来这只作品。 – 2010-06-01 22:20:45
真棒!非常有用。 – professorDante 2014-05-05 01:07:17
另请注意,'else'是必需的,不能被忽略,因为总是需要结果值。 – sebix 2014-08-05 08:51:22