如何更容易地或在一行中编写此代码段?
dist=[]
for k in range(5):
dist.append(k)
dist[0]="Apple"
print(dist)
答案 0 :(得分:5)
>>> dist = ['Apple', *range(1, 5)]
>>> dist
['Apple', 1, 2, 3, 4]
答案 1 :(得分:1)
Python 2.7(也适用于Python 3+):
>>> dist = ['Apple'] + [i for i in range(1,5)]
>>> dist
['Apple', 1, 2, 3, 4]