【问题标题】:Getting IndexError Traceback (most recent call last) for Existing index获取现有索引的 IndexError Traceback(最近一次调用最后一次)
【发布时间】:2020-08-18 01:17:22
【问题描述】:

我将此函数用于两个数据集(iOS_list 和 play_apps)。两个数据集都是包含字符串列表的列表。下面的函数适用于 play_apps 变量,但 iOS_list 变量返回一个 IndexError。我可以访问下面一行函数中引用的索引:

print(iOS_list[0][2])

所以我不确定为什么我在 iOS_list 变量上使用函数时总是出错。

def find_duplicates(listy, index): 
    unique_apps = [] # list of all app names
    duplicate_apps = [] # list of known duplicate app names

    for app in listy:
        name = app[index]
        if name in unique_apps: 
            duplicate_apps.append(name) 
        elif name not in unique_apps: 
            unique_apps.append(name)
    print('Count of duplicate apps in data set:',len(duplicate_apps))
    print('\n')
    print('Sample of duplicate apps in data set:', duplicate_apps[:3])
    print('\n')    

find_duplicates(play_apps,0)
find_duplicates(iOS_list,2)

两个数据集的第一行样本:

print(iOS_list[:1],'\n')
print(play_apps[:1])
print(type(iOS_list[0]))
print(type(play_apps[0]))

[['1', '281656475', 'PAC-MAN Premium', '100788224', 'USD', '3.99', '21292', '26', '4', '4.5', '6.3.5', '4+', 'Games', '38', '5', '10', '1']] 

[['Photo Editor & Candy Camera & Grid & ScrapBook', 'ART_AND_DESIGN', '4.1', '159', '19M', '10,000+', 'Free', '0', 'Everyone', 'Art & Design', 'January 7, 2018', '1.0.0', '4.0.3 and up']]

<class 'list'>
<class 'list'>

预期输出(这适用于 play_apps 变量):

Count of duplicate apps in data set: 1181


Sample of duplicate apps in data set: ['Quick PDF Scanner + OCR FREE', 'Box', 'Google My Business']

实际输出(仅适用于 iOS_list 变量):

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-49-91240e63afea> in <module>
     29 
     30 
---> 31 find_duplicates(iOS_list,2)
     32 
     33 

<ipython-input-49-91240e63afea> in find_duplicates(listy, index)
      4 
      5     for app in listy:
----> 6         name = app[index]
      7         if name in unique_apps: # if app name is already in the unique_apps list
      8             duplicate_apps.append(name) # add that app name to the duplicate_apps list

IndexError: list index out of range

抱歉,如果这是一个愚蠢的问题,我是新手,无法找到解决这个问题的方法。

【问题讨论】:

  • 你能分享更多细节吗?也许您的列表或预期和实际输出
  • 当然,我已经添加了每个变量的第一行。
  • 你能发布完整的错误回溯吗?
  • 糟糕,误解了最初的请求。是的,刚刚发布。

标签: python python-3.x index-error


【解决方案1】:

欢迎来到 Stack Overflow。当我运行您的代码时,我没有收到任何错误,但由于没有任何重复,因此两个数据集的计数结果均为 0。所以我改变了你的输入数据:

iOS_list = [
    ["Village", "did", "removed", "enjoyed", "explain", "nor", "ham", "saw", "calling", "talking."], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["feelings", "own", "shy.", "Request", "norland", "neither", "mistake", "for", "yet.", "Between"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["have", "an", "no", "at.", "Relation", "so", "in", "confined", "smallest", "children"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["account", "in", "outward", "tedious", "do.", "Particular", "way", "thoroughly", "unaffected", "projection"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["unpacked", "delicate.", "Why", "sir", "end", "believe", "uncivil", "respect.", "Always", "get"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["adieus", "nature", "day", "course", "for", "common.", "My", "little", "garret", "repair"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["have", "an", "no", "at.", "Relation", "so", "in", "confined", "smallest", "children"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["For", "who", "thoroughly", "her", "boy", "estimating", "conviction.", "Removed", "demands", "expense"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["account", "in", "outward", "tedious", "do.", "Particular", "way", "thoroughly", "unaffected", "projection"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["admire", "in", "giving.", "See", "resolved", "goodness", "felicity", "shy", "civility", "domestic"], 
    ["had", "but.", "Drawings", "offended", "yet", "answered", "jennings", "perceive", "laughing", "six"]
]
play_apps = [
    ["We", "diminution", "preference", "thoroughly", "if.", "Joy", "deal", "pain", "view", "much"], 
    ["her", "time.", "Led", "young", "gay", "would", "now", "state.", "Pronounce", "we"], 
    ["unlocked", "entirely", "mr", "relation", "diverted", "in.", "Known", "not", "end", "fully"], 
    ["attention", "admitting", "on", "assurance", "of", "suspicion", "conveying.", "That", "his", "west"], 
    ["quit", "had", "met", "till.", "Of", "advantage", "he", "attending", "household", "at"], 
    ["do", "perceived.", "Middleton", "in", "objection", "discovery", "as", "agreeable.", "Edward", "thrown"], 
    ["unlocked", "entirely", "mr", "relation", "diverted", "in.", "Known", "not", "end", "fully"], 
    ["dining", "so", "he", "my", "around", "to.", "Increasing", "impression", "interested", "expression"], 
    ["he", "my", "at.", "Respect", "invited", "request", "charmed", "me", "warrant", "to."], 
    ["Expect", "no", "pretty", "as", "do", "though", "so", "genius", "afraid", "cousin."], 
    ["do", "perceived.", "Middleton", "in", "objection", "discovery", "as", "agreeable.", "Edward", "thrown"], 
    ["Girl", "when", "of", "ye", "snug", "poor", "draw.", "Mistake", "totally", "of"], 
    ["in", "chiefly.", "Justice", "visitor", "him", "entered", "for.", "Continue", "delicate", "as"], 
    ["unlocked", "entirely", "mr", "relation", "diverted", "in.", "Known", "not", "end", "fully"], 
    ["being", "style", "house.", "An", "whom", "down", "kept", "lain", "name", "so"], 
]

def find_duplicates(listy, index): 
    unique_apps = [] # list of all app names
    duplicate_apps = [] # list of known duplicate app names

    for app in listy:
        name = app[index]
        if name in unique_apps: 
            duplicate_apps.append(name) 
        elif name not in unique_apps: 
            unique_apps.append(name)
    print('Count of duplicate apps in data set:',len(duplicate_apps))
    print('\n')
    print('Sample of duplicate apps in data set:', duplicate_apps[:3])
    print('\n')    

find_duplicates(play_apps, 0)
find_duplicates(iOS_list, 2)

它仍在运行,没有任何问题,所以如果您报告了错误,我认为您可能需要检查您的输入数据。我认为您可能有一个列表(在您的列表中)其中没有 3 个元素(因为您正在查看索引 2)

但我也发现了可能的错误:在您的代码中,您将重复的应用名称与唯一的应用名称分开。但是,如果一个名字出现了 5 次,则一个副本将在 unique_apps 中,四个副本将在 duplicate_apps 中。我可能错了,但我认为您希望该名称仅在 duplicate_apps 中出现一次。如果这是您想要的,您可以通过使用 set 代替列表来对您的代码进行最小的更改 duplicate_apps,这样您就不会有重复项。

iOS_list = [
    ["Village", "did", "removed", "enjoyed", "explain", "nor", "ham", "saw", "calling", "talking."], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["feelings", "own", "shy.", "Request", "norland", "neither", "mistake", "for", "yet.", "Between"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["have", "an", "no", "at.", "Relation", "so", "in", "confined", "smallest", "children"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["account", "in", "outward", "tedious", "do.", "Particular", "way", "thoroughly", "unaffected", "projection"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["unpacked", "delicate.", "Why", "sir", "end", "believe", "uncivil", "respect.", "Always", "get"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["adieus", "nature", "day", "course", "for", "common.", "My", "little", "garret", "repair"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["have", "an", "no", "at.", "Relation", "so", "in", "confined", "smallest", "children"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["For", "who", "thoroughly", "her", "boy", "estimating", "conviction.", "Removed", "demands", "expense"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["account", "in", "outward", "tedious", "do.", "Particular", "way", "thoroughly", "unaffected", "projection"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["admire", "in", "giving.", "See", "resolved", "goodness", "felicity", "shy", "civility", "domestic"], 
    ["had", "but.", "Drawings", "offended", "yet", "answered", "jennings", "perceive", "laughing", "six"]
]
play_apps = [
    ["We", "diminution", "preference", "thoroughly", "if.", "Joy", "deal", "pain", "view", "much"], 
    ["her", "time.", "Led", "young", "gay", "would", "now", "state.", "Pronounce", "we"], 
    ["unlocked", "entirely", "mr", "relation", "diverted", "in.", "Known", "not", "end", "fully"], 
    ["attention", "admitting", "on", "assurance", "of", "suspicion", "conveying.", "That", "his", "west"], 
    ["quit", "had", "met", "till.", "Of", "advantage", "he", "attending", "household", "at"], 
    ["do", "perceived.", "Middleton", "in", "objection", "discovery", "as", "agreeable.", "Edward", "thrown"], 
    ["unlocked", "entirely", "mr", "relation", "diverted", "in.", "Known", "not", "end", "fully"], 
    ["dining", "so", "he", "my", "around", "to.", "Increasing", "impression", "interested", "expression"], 
    ["he", "my", "at.", "Respect", "invited", "request", "charmed", "me", "warrant", "to."], 
    ["Expect", "no", "pretty", "as", "do", "though", "so", "genius", "afraid", "cousin."], 
    ["do", "perceived.", "Middleton", "in", "objection", "discovery", "as", "agreeable.", "Edward", "thrown"], 
    ["Girl", "when", "of", "ye", "snug", "poor", "draw.", "Mistake", "totally", "of"], 
    ["in", "chiefly.", "Justice", "visitor", "him", "entered", "for.", "Continue", "delicate", "as"], 
    ["unlocked", "entirely", "mr", "relation", "diverted", "in.", "Known", "not", "end", "fully"], 
    ["being", "style", "house.", "An", "whom", "down", "kept", "lain", "name", "so"], 
]

def find_duplicates(listy, index): 
    unique_apps = [] # list of all app names
    duplicate_apps = set() # list of known duplicate app names

    for app in listy:
        name = app[index]
        if name in unique_apps: 
            duplicate_apps.add(name) 
        elif name not in unique_apps: 
            unique_apps.append(name)
    print('Count of duplicate apps in data set:',len(duplicate_apps))
    print('\n')
    print('Sample of duplicate apps in data set:', list(duplicate_apps)[:3])
    print('\n')    

find_duplicates(play_apps, 0)
find_duplicates(iOS_list, 2)

但我们可以通过使用generator expressionCounterlist comprehensionrandom.sample() 函数来简化您的代码。

import collections
import random

iOS_list = [
    ["Village", "did", "removed", "enjoyed", "explain", "nor", "ham", "saw", "calling", "talking."], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["feelings", "own", "shy.", "Request", "norland", "neither", "mistake", "for", "yet.", "Between"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["have", "an", "no", "at.", "Relation", "so", "in", "confined", "smallest", "children"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["account", "in", "outward", "tedious", "do.", "Particular", "way", "thoroughly", "unaffected", "projection"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["unpacked", "delicate.", "Why", "sir", "end", "believe", "uncivil", "respect.", "Always", "get"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["adieus", "nature", "day", "course", "for", "common.", "My", "little", "garret", "repair"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["have", "an", "no", "at.", "Relation", "so", "in", "confined", "smallest", "children"], 
    ["Securing", "as", "informed", "declared", "or", "margaret.", "Joy", "horrible", "moreover", "man"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["the", "for", "morning", "assured", "country", "believe.", "On", "even", "feet", "time"], 
    ["For", "who", "thoroughly", "her", "boy", "estimating", "conviction.", "Removed", "demands", "expense"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["account", "in", "outward", "tedious", "do.", "Particular", "way", "thoroughly", "unaffected", "projection"], 
    ["favourable", "mrs", "can", "projecting", "own.", "Thirty", "it", "matter", "enable", "become"], 
    ["admire", "in", "giving.", "See", "resolved", "goodness", "felicity", "shy", "civility", "domestic"], 
    ["had", "but.", "Drawings", "offended", "yet", "answered", "jennings", "perceive", "laughing", "six"]
]
play_apps = [
    ["We", "diminution", "preference", "thoroughly", "if.", "Joy", "deal", "pain", "view", "much"], 
    ["her", "time.", "Led", "young", "gay", "would", "now", "state.", "Pronounce", "we"], 
    ["unlocked", "entirely", "mr", "relation", "diverted", "in.", "Known", "not", "end", "fully"], 
    ["attention", "admitting", "on", "assurance", "of", "suspicion", "conveying.", "That", "his", "west"], 
    ["quit", "had", "met", "till.", "Of", "advantage", "he", "attending", "household", "at"], 
    ["do", "perceived.", "Middleton", "in", "objection", "discovery", "as", "agreeable.", "Edward", "thrown"], 
    ["unlocked", "entirely", "mr", "relation", "diverted", "in.", "Known", "not", "end", "fully"], 
    ["dining", "so", "he", "my", "around", "to.", "Increasing", "impression", "interested", "expression"], 
    ["he", "my", "at.", "Respect", "invited", "request", "charmed", "me", "warrant", "to."], 
    ["Expect", "no", "pretty", "as", "do", "though", "so", "genius", "afraid", "cousin."], 
    ["do", "perceived.", "Middleton", "in", "objection", "discovery", "as", "agreeable.", "Edward", "thrown"], 
    ["Girl", "when", "of", "ye", "snug", "poor", "draw.", "Mistake", "totally", "of"], 
    ["in", "chiefly.", "Justice", "visitor", "him", "entered", "for.", "Continue", "delicate", "as"], 
    ["unlocked", "entirely", "mr", "relation", "diverted", "in.", "Known", "not", "end", "fully"], 
    ["being", "style", "house.", "An", "whom", "down", "kept", "lain", "name", "so"], 
]

def find_duplicates(source_list, name_location):
    names = (current_app[name_location] for current_app in source_list)
    counts = collections.Counter(names)
    duplicates = [name for (name, count) in counts.items() if count > 1]
    duplicates_count = len(duplicates)
    sample  = random.sample(duplicates, min(3, duplicates_count))

    print('Count of duplicate apps in data set:', duplicates_count, '\n')
    print('Sample of duplicate apps in data set:', sample, '\n')

find_duplicates(play_apps, 0)
find_duplicates(iOS_list, 2)

你满意吗?

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    相关资源
    最近更新 更多