简体   繁体   English

枚举-Python循环

[英]Enumerate - Python loop

I have two lists with just one element as follows(each of these two lists always contains only one element): 我有两个只有一个元素的列表,如下所示(这两个列表中的每一个始终仅包含一个元素):

Vnew = [600]
Newoints2 = [(2447,3480)]

I'm trying to bring both of them together using the following code sample: 我正在尝试使用下面的代码示例将它们放在一起:

for i, key in enumerate(Vnew2):
  pos3[key] = newpoints2[i]

But this returns an error as IndexError: list assignment index out of range 但这会返回一个错误,作为IndexError: list assignment index out of range

I actually did this one for other lists which have more than one element. 实际上,我对其他包含多个元素的列表进行了此操作。 It worked fine and got the output as {0:(1245,674),1:(2457,895),...} 它工作正常,输出为{0:(1245,674),1:(2457,895),...}

Can someone find the mistake here? 有人可以在这里找到错误吗?

It looks like you are trying to concatenate the lists into a new list. 看来您正在尝试将列表连接成一个新列表。 You don't always need to enumerate through the list. 您不必总是通过列表进行枚举。

You will be able to do this by Vnew + Newoints2 您将可以通过Vnew + Newoints2完成此Vnew + Newoints2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM