這裏是我試圖變成一個列表理解的代碼:是否可以在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