简体   繁体   中英

python iterate over a list and remove any duplicates found within lists inside a dictionary and remove the duplicates

i have a list of numbers and a dictionary with names(key) and numbers(values) i need to iterate over the list called lotto and and then check them against the values of the names in the dictionary and if i get a match remove that number from the dictionary value.

heres what i have so far but it just prints out the origanal dictionary keys and values

players = {'ray': [1,2,3,6,7,8,], 'al':[1,2,3,4,8,9,]}
lotto =[1,2,3,4,5,6,]

for i in players.values():  
    if i in lotto:
       players.values.remove(i)  

print (players)

any help appriciated

You are accessing the dict values in a wrong way. Take a look at the following:

players = {'ray': [1,2,3,6,7,8,], 'al':[1,2,3,4,8,9,]}
lotto =[1,2,3,4,5,6,]

for p, val in players.items():  
    for num in lotto:
        if num in val:
            players[p].remove(num)

print (players)  # {'al': [8, 9], 'ray': [7, 8]}

If the order of the items in the list-values is not important to you, you can use the following, faster, variant:

players = {'ray': [1,2,3,6,7,8], 'al':[1,2,3,4,8,9]}
lotto = [1,2,3,4,5,6]
lotto = set(lotto)

for p, val in players.items():
    players[p] = list(set(val) - lotto)

print (players)  # {'ray': [8, 7], 'al': [8, 9]}

which can also be condensed in the following one-liner (it recreates the dict though instead of modifying it):

players = {k: list(set(val) - lotto) for k, v in players.items()}

Your problem is that your trying to remove your numbers from the wrong list. players.values is a list of each key's value , not each key's individual value . Use each key's individual value instead:

players = {'ray': [1,2,3,6,7,8,], 'al':[1,2,3,4,8,9,]}
lotto = [1,2,3,4,5,6,]

for key, value in players.items():
    for i in lotto:
        if i in value:
            value.remove(i)

print(players)

Then can be shortened however by using a dictionary comprehension:

players = {'ray': [1,2,3,6,7,8,], 'al':[1,2,3,4,8,9,]}
lotto =[1,2,3,4,5,6,]

players = {k: list(set(v) - set(lotto)) for k, v in players.items()}
print(players) # {'ray': [8, 7], 'al': [8, 9]}

Also, if the order of the list in your program doesn't matter, I recommend you use set s instead. They provide efficient element lookup, and make doing things like this very simple:

>>> players = {'ray': {1,2,3,6,7,8,}, 'al':{1,2,3,4,8,9,}}
>>> lotto = {1,2,3,4,5,6,}
>>> {k: v - lotto for k, v in players.items()}
{'ray': {8, 7}, 'al': {8, 9}}
>>> 

You can filter like so:

players = {'ray': [1,2,3,6,7,8,], 'al':[1,2,3,4,8,9,]}

lotto =[1,2,3,4,5,6]

new_players = {a:[i for i in b if i not in lotto] for a, b in players.items()}

Output:

{'al': [8, 9], 'ray': [7, 8]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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