简体   繁体   English

if语句中的Python列表索引错误

[英]Python list index error in an if statement

I am not sure why I am getting this list index out of bounds error 我不确定为什么我会超出列表索引错误

Basically what is supposed to happen is I am sending my def a list of twitter userIds and then breaking them into chunks of 100 looking them up in twitter, then adding them to a dictionary using the userIds as the key. 基本上应该发生的是,我向def发送一个twitter用户ID列表,然后将它们分成100个块,在Twitter中查找它们,然后使用userId作为键将它们添加到字典中。 So lets say 00001 is johnny we look up 00001 get johnny and then make a dictionary with 00001, johnny. 因此,假设00001是johnny,我们查找00001以获得johnny,然后用00001,johnny制作字典。 However the if statements don't seem to trigger. 但是,if语句似乎没有触发。

Here is the code: 这是代码:

 def getUserName(lookupIds):
     l = len(lookupIds) # length of list to process
     i = 0 #setting up increment for while loop 
     screenNames = {}#output dictionary
     count = 0 #count of total numbers processed
     print lookupIds
     while i < l:
         toGet = []
         if l - count > 100:#blocks off in chunks of 100
             for m  in range (0,100):
                toGet[m] = lookupIds[count]
                count = count + 1
                print toGet
         else:#handles the remainder 
              r = l - count 
              print screenNames
              for k  in range (0,r):#takes the remainder of the numbers 
                  toGet[k] = lookupIds[count]
                  count = count + 1
              i = l   # kills loop

          screenNames.update(zip(toGet, api.lookup_users(user_ids=toGet)))
          #creates a dictionary screenNames{user_Ids, screen_Names}

     #This logic structure breaks up the list of numbers in chunks of 100 or their
     #Remainder and addes them into a dictionary with their userID number as the 
     #index value Count is for monitoring how far the loop has been progressing.    
     print len(screenNames) + 'screen names correlated'
     return screenNames

The error is as follows: 错误如下:

Traceback (most recent call last):
  File "twitterBot2.py", line 78, in <module>
    toPrint = getUserName(followingids)#Testing Only
  File "twitterBot2.py", line 42, in getUserName
    toGet[k] = lookupIds[count]
IndexError: list assignment index out of range

toGet is initialized to the empty list, and you're attempting to assign [0] a value. toGet初始化为空列表,并且您试图分配[0]值。 This is illegal. 这是非法的。 Use append instead: 请改用append:

toGet.append(lookupIds[count])

This is likely because you're attempting to lookup index zero when it doesn't exist. 这可能是因为您正在尝试查找不存在的索引零。 Example: 例:

>>> x=[]
>>> x[0] = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
def getUserName(lookUpIds):
    blockSize = 100
    screenNames = {}
    indexes = xrange(0, len(lookUpIds), blockSize)
    blocks = [lookUpIds[i:(i + blockSize)] for i in indexes]
    for block in blocks:
        users = api.lookup_users(user_ids=block)
        screenNames.update(zip(block, users))

    return screenNames

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

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