我正在研究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