简体   繁体   English

在条件下删除 python 字典中的项目

[英]Removing items in a python dictionary under a condition

Suppose I have this dictionary假设我有这本词典

{0: ['replaced scanner user properly working cage replaced wire damaged stored later use',
  'replaced scanner'],
 1: ['scanner replaced station working well'],
 2: ['station scanner synced base station working'],
 3: ['printer offlineswitched usb portprinter online working'],
 4: ['replaced barcode label reflecting tcs ip address'],
 5: ['restarted thin client using ssh run restart command'],
 6: ['printer reconfigured test functioning normally', 'printer reconfigured'],
 7: ['removed old printer service installed replacement tested good'],
 8: ['tc required reboot rebooted tc aa signin dp functional'],
 9: ['resetting printer factory settings reconfigure'],
 10: ['updated windows forced update laptop operated normally'],
 11: ['printer settings set correct printer working correctly'],
 12: ['power printer disconnected reconnected working fine'],
 13: ['power cycled equipment restocked spooler plastic bubbles'],
 14: ['laptop checked ive logged paskiplacowepl without problem'],
 15: ['reseated scanner cables connection usb port resolve issue'],
 16: ['red scanner reconfigured base rebooted via usb joint'],
 17: ['tested printer station connected working resolved'],
 18: ['reconfigured printer media print properly'],
 19: ['replaced defective device fresh imaged laptop'],
 20: ['reconfigured scanner base tested aa fine'],
 21: ['printer unplugged usb port working properly'],
 22: ['reimaging laptop corrected anyconnect software issue'],
 23: ['reconfigured scanner confirmed scans intended'],
 24: ['reconnected usb cable reassign printer ports port'],
 25: ['plugged usb cable port scanner works'],
 26: ['cleared linux print queue working'],
 27: ['user reset password successfully closing tt'],
 28: ['reset printer get print'],
 29: ['reconfigured hand scanner linked station'],
 30: ['replaced scanner station functional', 'replaced scanner'],
 31: ['laptops battery needed reset asset serial'],
 32: ['printer reconfigured'],
 33: ['upgraded rebooted station'],
 34: ['deploying replacement scanner'],
 35: ['replaced scanner'],
 36: ['updated pc'],
 37: ['tc reimage']}

You can see that the key 0 and 35 have the same string.您可以看到键035具有相同的字符串。 I would like to have 35 removed.我想删除35个。 How would I generalize this process in the best pythonic way?我将如何以最好的 Python 方式概括这个过程?

You can create a list of all the values and check if the value already exits in the list.您可以创建所有值的列表并检查该值是否已存在于列表中。 If yes, then remove it from the values list.如果是,则将其从值列表中删除。 Replace the original list with the values.用值替换原始列表。 If you find an empty list, then store the key so you can pop it out from the dictionary after you finish iterating over it.如果找到一个空列表,则存储该键,以便在完成迭代后将其从字典中弹出。

#dict_vals has the original list.

consolidated_vals = [] # to store all values from the list
popkeys = [] #to store keys that need to be deleted

#iterate thru the full dictionary
for k,vals in dict_vals.items():
    
    #check if each value in the values list was already in the list. if found, ignore
    x = [v for v in vals if v not in consolidated_vals]
   
    #if list is empty (all values were already found in the list earler), then store key to pop
    if x:
        consolidated_vals += x  #store new values to the consolidated values list
        dict_vals[k] = x  #replace old values with new values as there may have been a value removed
    else:
        popkeys.append(k)  #store key to pop after iterating thru full list

#now pop all the keys that had 100% match for values already found

for pkeys in popkeys: dict_vals.pop(pkeys)

print (dict_vals)

The output of this will be: output 将是:

{0: ['replaced scanner user properly working cage replaced wire damaged stored later use', 'replaced scanner'], 
 1: ['scanner replaced station working well'], 
 2: ['station scanner synced base station working'], 
 3: ['printer offlineswitched usb portprinter online working'], 
 4: ['replaced barcode label reflecting tcs ip address'], 
 5: ['restarted thin client using ssh run restart command'], 
 6: ['printer reconfigured test functioning normally', 'printer reconfigured'], 
 7: ['removed old printer service installed replacement tested good'], 
 8: ['tc required reboot rebooted tc aa signin dp functional'], 
 9: ['resetting printer factory settings reconfigure'], 
10: ['updated windows forced update laptop operated normally'], 
11: ['printer settings set correct printer working correctly'], 
12: ['power printer disconnected reconnected working fine'], 
13: ['power cycled equipment restocked spooler plastic bubbles'], 
14: ['laptop checked ive logged paskiplacowepl without problem'], 
15: ['reseated scanner cables connection usb port resolve issue'], 
16: ['red scanner reconfigured base rebooted via usb joint'], 
17: ['tested printer station connected working resolved'], 
18: ['reconfigured printer media print properly'], 
19: ['replaced defective device fresh imaged laptop'], 
20: ['reconfigured scanner base tested aa fine'], 
21: ['printer unplugged usb port working properly'], 
22: ['reimaging laptop corrected anyconnect software issue'], 
23: ['reconfigured scanner confirmed scans intended'], 
24: ['reconnected usb cable reassign printer ports port'], 
25: ['plugged usb cable port scanner works'], 
26: ['cleared linux print queue working'], 
27: ['user reset password successfully closing tt'], 
28: ['reset printer get print'], 
29: ['reconfigured hand scanner linked station'], 
30: ['replaced scanner station functional'], 
31: ['laptops battery needed reset asset serial'], 
33: ['upgraded rebooted station'], 
34: ['deploying replacement scanner'], 
36: ['updated pc'], 
37: ['tc reimage']}

Note here that keys 32 and 35 were removed.注意这里删除了键 32 和 35。

Considering you just want to remove identical values, you could do some dict comprehensions like this:考虑到您只想删除相同的值,您可以像这样执行一些 dict 理解:

temp = {val : key for key, val in your_dict.items()} 
res = {val : key for key, val in temp.items()}
print(res)

Note that this will remove the entire key:val from the dictionary if the values are the same.请注意,如果相同,这将从字典中删除整个 key:val。

You can use the issubset method to find if there are any duplicates repeating in the list and then store the key values in a toBeRemoved list which have to be deleted.您可以使用 issubset 方法查找列表中是否有重复的重复项,然后将键值存储在必须删除的 toBeRemoved 列表中。

dict1 = {0: ['replaced scanner user properly working cage replaced wire damaged stored later use',
  'replaced scanner'],
 1: ['scanner replaced station working well'],
 2: ['station scanner synced base station working'],
 3: ['printer offlineswitched usb portprinter online working'],
 4: ['replaced barcode label reflecting tcs ip address'],
 5: ['restarted thin client using ssh run restart command'],
 6: ['printer reconfigured test functioning normally', 'printer reconfigured'],
 7: ['removed old printer service installed replacement tested good'],
 8: ['tc required reboot rebooted tc aa signin dp functional'],
 9: ['resetting printer factory settings reconfigure'],
 10: ['updated windows forced update laptop operated normally'],
 11: ['printer settings set correct printer working correctly'],
 12: ['power printer disconnected reconnected working fine'],
 13: ['power cycled equipment restocked spooler plastic bubbles'],
 14: ['laptop checked ive logged paskiplacowepl without problem'],
 15: ['reseated scanner cables connection usb port resolve issue'],
 16: ['red scanner reconfigured base rebooted via usb joint'],
 17: ['tested printer station connected working resolved'],
 18: ['reconfigured printer media print properly'],
 19: ['replaced defective device fresh imaged laptop'],
 20: ['reconfigured scanner base tested aa fine'],
 21: ['printer unplugged usb port working properly'],
 22: ['reimaging laptop corrected anyconnect software issue'],
 23: ['reconfigured scanner confirmed scans intended'],
 24: ['reconnected usb cable reassign printer ports port'],
 25: ['plugged usb cable port scanner works'],
 26: ['cleared linux print queue working'],
 27: ['user reset password successfully closing tt'],
 28: ['reset printer get print'],
 29: ['reconfigured hand scanner linked station'],
 30: ['replaced scanner station functional', 'replaced scanner'],
 31: ['laptops battery needed reset asset serial'],
 32: ['printer reconfigured'],
 33: ['upgraded rebooted station'],
 34: ['deploying replacement scanner'],
 35: ['replaced scanner'],
 36: ['updated pc'],
 37: ['tc reimage']}

toBeRemoved=set()
for x in dict1:
    for y in dict1:
        if(x!=y):
            if(set(dict1[x]).issubset(set(dict1[y]))):
                toBeRemoved.add(x)
for item in toBeRemoved:
    del dict1[item]
print(dict1)

Output Output

{0: ['replaced scanner user properly working cage replaced wire damaged stored later use', 'replaced scanner'], 1: ['scanner replaced station working well'], 2: ['station scanner synced base station working'], 3: ['printer offlineswitched usb portprinter online working'], 4: ['replaced barcode label reflecting tcs ip address'], 5: ['restarted thin client using ssh run restart command'], 6: ['printer reconfigured test functioning normally', 'printer reconfigured'], 7: ['removed old printer service installed replacement tested good'], 8: ['tc required reboot rebooted tc aa signin dp functional'], 9: ['resetting printer factory settings reconfigure'], 10: ['updated windows forced update laptop operated normally'], 11: ['printer settings set correct printer working correctly'], 12: ['power printer disconnected reconnected working fine'], 13: ['power cycled equipment restocked spooler plastic bubbles'], 14: ['laptop checked ive logged paskiplacowepl without problem'], 15: ['reseated scanner cables connection usb port resolve issue'], 16: ['red scanner reconfigured base rebooted via usb joint'], 17: ['tested printer station connected working resolved'], 18: ['reconfigured printer media print properly'], 19: ['replaced defective device fresh imaged laptop'], 20: ['reconfigured scanner base tested aa fine'], 21: ['printer unplugged usb port working properly'], 22: ['reimaging laptop corrected anyconnect software issue'], 23: ['reconfigured scanner confirmed scans intended'], 24: ['reconnected usb cable reassign printer ports port'], 25: ['plugged usb cable port scanner works'], 26: ['cleared linux print queue working'], 27: ['user reset password successfully closing tt'], 28: ['reset printer get print'], 29: ['reconfigured hand scanner linked station'], 30: ['replaced scanner station functional', 'replaced scanner'], 31: ['laptops battery needed reset asset serial'], 33: ['upgraded rebooted station'], 34: ['deploying replacement scanner'], 36: ['updated pc'], 37: ['tc reimage']}

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

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