[問題] 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