[问题] Single statement question

楼主: meld (mind)   2014-02-22 23:59:51
请问在这份投影片第54页的练习要如何全部缩为一行呢?
http://www.slideshare.net/JustinSDK/pycon-taiwan-2013-tutorial
原始内容如下:
numbers = []
for number in range(20):
numbers.append(str(number))
print ", ".join(numbers)
只知道前面的叙述可以先简写成这样:
[numbers.append(str(number)) for number in range(20)]
但是最后一行的 join 不晓得如何整合进去,请高手指点,谢谢!
作者: uranusjr (←這人是超級笨蛋)   2014-02-23 00:09:00
脑子卡在 imperative 模式了, 请转个弯numbers = [str(number) for number in range(20)]这样要把两行并一行应该很直觉吧?
楼主: meld (mind)   2014-02-23 00:14:00
print (", ".join([str(number) for number in range(20)]))谢谢大师!
作者: doglegbow (堡)   2014-02-23 00:14:00
print ", ".join([str(number) for number in range(20)]) id太长=.=
楼主: meld (mind)   2014-02-23 00:16:00
XD 也谢谢楼上高手!
作者: darkgerm (黑骏)   2014-02-23 12:53:00
中括号可以去掉

Links booklink

Contact Us: admin [ a t ] ucptt.com