简体   繁体   English

错误列表索引必须是整数,而不是列表。 将一个数组的值用作索引以从另一个数组中删除值

[英]error list indices must be integers, not list. Take values of one array use them as indexes to delete values from another array

seen = []
dups = collections.defaultdict(list)
for i, item in enumerate(prules)
    for j, orig in enumerate(seen):
        if item == orig:
            dups[j].append(i)
            break
    else:
        seen.append(item)
deleteindex = [val for key,val in dups.iteritems() if seen[key] == '159']
for i in range(o,len(deleteindex)):
    n = deleteindex[i]
    del rulelines[n]

The above is my code. 以上是我的代码。

What I want to do is to create an array deleteindex which takes in the indice of any item with 159 in. 我想要做的是创建一个数组deleteindex ,该数组接受159 in中任何项目的deleteindex

It does get all the values I want ie all the indices with the value 159 but when I try to delete the values from a different array with the indices, it returns the error 它确实获得了我想要的所有值,即所有具有值159的索引,但是当我尝试从具有索引的不同数组中删除值时,它返回错误

list indices must be integers, not list. 列表索引必须是整数,而不是列表。

prules is the array I want to get the index values contains strings of numbers prules是我要获取的索引值包含数字字符串的数组
rulelines contains is a list of strings which I want to use the values taken from prules and use the values as indexes to delete those values in rulelines rulelines包含是我想用取自值的字符串列表prules ,并使用值作为指标来删除这些值rulelines

Where have I gone wrong? 我哪里出问题了?
I'm guessing its something at 我猜它在

deleteindex = [val for key,val in dups.iteritems() if seen[key] == '159']

You are right. 你是对的。
The error is because, in 错误是因为

deleteindex = [val for key,val in dups.iteritems() if seen[key] == '159']

key is a list . key是一个list As dups is defaultdict(list) , the value will always be a list . 由于dupsdefaultdict(list) ,因此该值将始终是list
This can be verified by: 可以通过以下方式验证:

...
print dict(dups.iteritems())  # adding this
deleteindex = [val for key,val in dups.iteritems() if seen[key] == '159']
...

As for your algorithm, try this, an easier to understand version. 至于您的算法,请尝试一下,这是一个更容易理解的版本。

import random

prules = [random.randint(150,156) for i in range(30)]

def get_indexes(li):
    retval = {}
    for i, x in enumerate(li):
        if x not in retval:
            retval[x] = []
        retval[x].append(i)
    return retval               

dups = get_indexes(prules)
indexes = dups.get('156',[])
rulelines = [rulelines[i] for i in range(len(rulelines[:])) if i not in indexes]

To get the indexes, just do: 要获取索引,只需执行以下操作:

dups.get('156',[])

Which will return a list of indexes if dups['156'] exists, else it returns an empty list. 如果存在dups['156'] ,它将返回索引列表,否则将返回一个空列表。

Your intuition is right. 你的直觉是对的。

deleteindex = [val for key,val in dups.iteritems() if seen[key] == '159']

Here each val is one of the values in the dups dictionary you've been populating in the above loop. 在这里,每个val是您在上述循环中填充的dups词典中的值之一。 But what are these values? 但是这些价值是什么? Well, dups = collections.defaultdict(list) would indicate that each one of these values is a list . 好吧, dups = collections.defaultdict(list)将指示这些值中的每一个都是一个list

Therefore this code breaks due to your not keeping track of your types: 因此,由于您无法跟踪类型,因此该代码中断了:

n = deleteindex[i]  # n is a list
del rulelines[n]    # you can't index a list with a list

From what I can tell, prules is a list, or a collection at least that can be enumerated, in which case, you can possibly convert it to a set which is all you need to do to remove duplicates. 从我所知道的, prules是一个列表,或至少一个集合,可以列举,在这种情况下,你都不可能将其转换为一个set这是所有你需要做的删除重复。


However, if this is not the case (I can not tell since you have not provided that segment of code) then you could go about the whole thing in a better way: 但是,如果不是这种情况(由于您没有提供该段代码,我就不能告诉您),那么您可以采用一种更好的方式处理整个问题:

seen = []
duplicate_indexes = []
for index, item in enumerate(prules):
    if item in seen:
        del rulelines[index]
    else:
        seen.append(item)

From your updated Question, I think that you are trying to do something else than what it seems your question is about. 从您更新的问题中,我认为您正在尝试做其他事情,而不是看起来似乎是什么。 I think you are trying to delete every index in rulelines where the corresponding index in prules has a value of '159' if this is the case: 我认为你正在试图删除各项指标rulelines其中相应的指标prules具有值'159' ,如果是这样的话:

for ix, pr in enumerate(prules):
    if pr == '159':
        del rulelines[ix]

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

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