如您所见,以下名为routine
的数组中有一系列其他数组。你知道吗
[['Dumbell Press', 'Chest Press Machine', 'Smith Machine Bench Press', 'Angled Dips'], [], [], [], ['Tricep Kickbacks', 'Overhead Dumbell Extensions'], [], []]
我已尝试将此数组中的每个项复制到新数组中。但是当我这样做的时候,我得到了这个输出和下面的错误消息。你知道吗
Bench Press
Inner Chest Push
Smith Machine Bench Press
Cable Crossover
IndexError: list index out of range
很明显,代码通过2d数组中的第一个数组工作,但是在那之后就停止了。你知道吗
这是用于生成上述错误消息的代码。你知道吗
newarray=[]
for x in range(len(routine)-1):
for i in range(len(routine)-1):
temp = routine[x][i]
print (temp)
newarray.append(temp)
有没有一种方法可以连接这些数组,这样就只有一个数组是这样的。你知道吗
['Dumbell Press', 'Chest Press Machine', 'Smith Machine Bench Press', 'Angled Dips','Tricep Kickbacks', 'Overhead Dumbell Extensions']
这就是你想要的:
你可以试试这个:
如果有嵌套的
list
,可以尝试使用列表理解:只有当你有
list
的lists
或两个级别的深度时,这才有效。你知道吗要更改代码,我们可以执行以下操作以获得相同的结果:
注意,我从代码中删除了
-1
。range(start, end)
从start
到end-1
也就是整个数组,因为数组从0
开始。也就是说,你不需要-1
。你知道吗相关问题 更多 >
编程相关推荐