简体   繁体   English

python遍历列表,并删除字典中列表内发现的所有重复项,并删除重复项

[英]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. 我有一个数字列表和一个带有名称(键)和数字(值)的字典,我需要遍历名为lotto的列表,然后对照字典中名称的值检查它们,如果我找到匹配项,则删除该列表字典值中的数字。

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. 您以错误的方式访问dict值。 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): 也可以在以下单行代码中进行压缩(尽管它可以重新创建dict而不是对其进行修改):

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 . players.values每个键的值的列表,而不是每个键的单个值的列表 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. 另外,如果程序中列表的顺序无关紧要,建议您改用set s。 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]}

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

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