代码片段列表Python更易于形式

2024-04-20 13:28:27 发布

您现在位置:Python中文网/ 问答频道 /正文

如何更容易地或一行地编写这些代码段

dist=[]
for k in range(5):
    dist.append(k)
dist[0]="Apple"
print(dist)

Tags: inapplefordist代码段rangeprintappend
2条回答
>>> dist = ['Apple', *range(1, 5)]
>>> dist
['Apple', 1, 2, 3, 4]

Python2.7(也适用于Python3+):

>>> dist = ['Apple'] + [i for i in range(1,5)]
>>> dist
['Apple', 1, 2, 3, 4]

相关问题 更多 >