我正在研究Learn Python The Hard Way PDF。在第82頁,我遇到了這個問題。'範圍'功能是否可以分配給列表?
- 你能避免完全在第23行的for-loop,只是直接指定範圍(0,6)到元素?
給予代碼:
# we can also build lists, first start with an empty one
elements = []
# then use the range function to do 0 to 20 counts
for i in range(0, 6):
print "Adding %d to the list." % i # line 23
# append is a function that lists understand
elements.append(i)
# now we can print them out too
for i in elements:
print "Element was: %d" % i
看來這是不可能的,除非我使用地圖功能?我對麼?
謝謝(大家)。我覺得太困難了。 – 2011-06-11 19:20:44
@Bas van der Zon - 這個答案只適用於Python 2.x,而不是3. – Omnifarious 2011-06-11 19:24:06