error list indices must be integers, not list. Tak

2020-05-01 09:29发布

问题:

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.

It does get all the values I want i.e 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

list indices must be integers, not list.

prules is the array I want to get the index values contains strings of numbers
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

Where have I gone wrong?
I'm guessing its something at

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

回答1:

You are right.
The error is because, in

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

key is a list. As dups is defaultdict(list), the value will always be a 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.



回答2:

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. But what are these values? Well, dups = collections.defaultdict(list) would indicate that each one of these values is a 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


回答3:

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.


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:

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