Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在范围内显示生成列表的部分?_Python_Python 3.x - Fatal编程技术网

Python 如何在范围内显示生成列表的部分?

Python 如何在范围内显示生成列表的部分?,python,python-3.x,Python,Python 3.x,我正在制作一个代码工作的数字列表生成器,但我想在第一行和最后一行显示第一个值(460)。有可能吗 以下是我可以用代码生成的内容 460-2-401-1 460-2-402-2 460-2-403-3 460-2-404-4 460-2-405-5 这是我想要的样子 460-2-401-1 2-402-2 2-403-3 2-404-4 460-2-405-5 这是到目前为止我的代码 steps = 0 for parts in range(5): print('{}-{}-{}-{}

我正在制作一个代码工作的数字列表生成器,但我想在第一行和最后一行显示第一个值(460)。有可能吗

以下是我可以用代码生成的内容

460-2-401-1

460-2-402-2

460-2-403-3

460-2-404-4

460-2-405-5

这是我想要的样子

460-2-401-1

2-402-2

2-403-3

2-404-4

460-2-405-5

这是到目前为止我的代码

steps  =  0
for parts in range(5):
    print('{}-{}-{}-{}\n'.format(460, 2, 401 + steps , parts+1))
    steps +=1
使用if语句——如果它是第一个或最后一个,则在开始处添加460-:

steps  =  0
for parts in range(5):
    print(('460-' if parts in [0, 4] else '') + '{}-{}-{}\n'.format(2, 401 + steps , parts+1))
    steps +=1
或者,如果您需要模块化:

steps  =  0
for parts in range(n):
    print(('460-' if parts in [0, n - 1] else '') + '{}-{}-{}\n'.format(2, 401 + steps , parts+1))
    steps +=1
使用if语句——如果它是第一个或最后一个,则在开始处添加460-:

steps  =  0
for parts in range(5):
    print(('460-' if parts in [0, 4] else '') + '{}-{}-{}\n'.format(2, 401 + steps , parts+1))
    steps +=1
或者,如果您需要模块化:

steps  =  0
for parts in range(n):
    print(('460-' if parts in [0, n - 1] else '') + '{}-{}-{}\n'.format(2, 401 + steps , parts+1))
    steps +=1

你必须定义n。在脚本前面加上“n=5”。为什么需要
步骤?只需将其替换为
零件
,并跳过赋值和增量<代码>用于范围(5)中的部件:打印('460-'如果部件在[0,4]中,则为其他“”)+'{}-{}-{}-{}\n'。格式(2,401+部件,部件+1))
这同样有效。@Idlehands有很好的优点。我假设OP在其他地方使用steps变量,但可能不是这样,你必须定义n。在脚本前面加上“n=5”。为什么需要
步骤?只需将其替换为
零件
,并跳过赋值和增量<代码>用于范围(5)中的部件:打印('460-'如果部件在[0,4]中,则为其他“”)+'{}-{}-{}-{}\n'。格式(2,401+部件,部件+1))
这同样有效。@Idlehands有很好的优点。我假设OP在其他地方使用steps变量,但情况可能并非如此。