remove element in list in a dictionary

2020-07-16 12:39发布

In a big dictionary, similar to

d = {}                                                                                   
d['a']=[1,2,3,4]                                                                         
d['b']=[1,2,3,4,5,6]                                                                     
d['c']=[1,2]                                                                             
d['d']=[1,4]

how can I quickly remove the 'four' in the lists ?

EDIT

Is there a way to link the fours in the lists? As in, eliminating one eliminates the others.

5条回答
家丑人穷心不美
2楼-- · 2020-07-16 13:06

You didn't mention what you want to happen if there are duplicates in your list. The solution I've written (iterate and then remove..) doesn't handle duplicates.

>>> d = {'a':[1,2,3,4],'b':[1,2,3,4,5,6],'c':[1,2],'d':[1,4],'e':[1,4,4]}
>>> for l in d.values():
...     if 4 in l: l.remove(4)
... 
>>> d
{'a': [1, 2, 3], 'c': [1, 2], 'b': [1, 2, 3, 5, 6], 'e': [1, 4], 'd': [1]}

This isn't very efficient though. The if 4 in l will iterate the list once, and the l.remove() will iterate the list again.

查看更多
来,给爷笑一个
3楼-- · 2020-07-16 13:09

It seems like you might benefit from making your dictionary "Symmetric", you could do something like this:

def make_symmetric(D):
    for key, value in D.items():
        for v in value:
            D.setdefault(v, set()).add(key)

def add(D, a, b):
    D.setdefault(a, set()).add(b)
    D.setdefault(b, set()).add(a)

def remove(D, a):
    values = D.pop(a)
    for v in values:
        D[v].remove(a)

And use it something like this:

>>> d = {'a': set([1, 2, 3, 4]),
         'b': set([1, 2, 3, 4, 5, 6]),
         'c': set([1, 2]),
         'd': set([1, 4])}
>>> make_symmetric(d)
>>> d
{1: set(['a', 'c', 'b', 'd']),
 2: set(['a', 'c', 'b']),
 3: set(['a', 'b']),
 4: set(['a', 'b', 'd']),
 5: set(['b']),
 6: set(['b']),
 'a': set([1, 2, 3, 4]),
 'b': set([1, 2, 3, 4, 5, 6]),
 'c': set([1, 2]),
 'd': set([1, 4])}
>>> remove(d, 4)
>>> d
{1: set(['a', 'c', 'b', 'd']),
 2: set(['a', 'c', 'b']),
 3: set(['a', 'b']),
 5: set(['b']),
 6: set(['b']),
 'a': set([1, 2, 3]),
 'b': set([1, 2, 3, 5, 6]),
 'c': set([1, 2]),
 'd': set([1])}
>>> add(d, 'd', 4)
>>> d
{1: set(['a', 'c', 'b', 'd']),
 2: set(['a', 'c', 'b']),
 3: set(['a', 'b']),
 4: set(['d']),
 5: set(['b']),
 6: set(['b']),
 'a': set([1, 2, 3]),
 'b': set([1, 2, 3, 5, 6]),
 'c': set([1, 2]),
 'd': set([1, 4])}

I use sets here, but you could do something similar with lists. I wouldn't be surprised if there was already some implementation of a "symmetric" dictionary somewhere. Hopefully someone else can point you in the right direction if it exists.

查看更多
The star\"
4楼-- · 2020-07-16 13:16

If values contain duplicates of 4, you could use something like this:

d = {}                                                                                   
d['a']=[1,2,3,4]                                                                         
d['b']=[1,2,3,4,4,5,6]                                                                     
d['c']=[1,2]                                                                             
d['d']=[1,4,4,4]

def remove_dup(x,n):
    while n in x:
        x.remove(n)
    return x

for ele in d.itervalues():
    try:
        remove_dup(ele,4)
    except ValueError:
        pass

Result:

>>> d
{'a': [1, 2, 3], 'c': [1, 2], 'b': [1, 2, 3, 5, 6], 'd': [1]}
查看更多
三岁会撩人
5楼-- · 2020-07-16 13:23

Iterate over the values of the dictionary and remove the 4 from every list:

for a in d.itervalues():
    try:
        a.remove(4)
    except ValueError:
        pass

This is not really efficient, since removing an element from a list is an O(n) operation. Use a different data type (e.g. a set) for better performance.

If the dictionary values are sets, you can do

for a in d.itervalues():
    a.discard(4)
查看更多
Fickle 薄情
6楼-- · 2020-07-16 13:23

Try this to remove all 4s:

for i in d:
    while 4 in d[i]:
       d[i].remove(4)

I don't know a way that you can eliminate all 4s at once.

查看更多
登录 后发表回答