Python Forum
remove duplicates from dicts with list values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
remove duplicates from dicts with list values
#21
Code usually doesn’t work when the requirements are too vague. Garbage in, garbage out.
Reply
#22
(May-27-2024, 01:16 PM)deanhystad Wrote: Code usually doesn’t work when the requirements are too vague. Garbage in, garbage out.

i dont think the requirement to look into two dictionaries which have lists as values and remove duplicate values qualifies as "vague". seems
more of a complex task which is not that easy as one/me might think, i even tried some ai python generators, no usable result from that corner also.
Reply
#23
If it is not vague why are there multiple solution attempts that interpret “duplicate” differently?
Reply
#24
no idea... duplicate is duplicate, nothing to interpret on this two deterministic expressions IMHO. must be some kind of internet frenzy.
Reply
#25
If a = {X:[y, z]} and b = {Y:[y, z]} are there any duplicates, or do the keys have to match to be a duplicate?
Is X a duplicate in this: a = {X:[]}, b = {X:[]}
Is y a duplicate in this: a = {X:[y]}, b={X:[[y]]}
"Duplicate" is very vague. Maybe it is clear to you since you defined what it means in your head, but you haven't shared that definition here.
Reply
#26
(May-27-2024, 02:57 PM)deanhystad Wrote: If a = {X:[y, z]} and b = {Y:[y, z]} are there any duplicates, or do the keys have to match to be a duplicate?
Is X a duplicate in this: a = {X:[]}, b = {X:[]}
Is y a duplicate in this: a = {X:[y]}, b={X:[[y]]}
"Duplicate" is very vague. Maybe it is clear to you since you defined what it means in your head, but you haven't shared that definition here.

If a = {X:[y, z]} and b = {Y:[y, z]} are there any duplicates, or do the keys have to match to be a duplicate?
as stated in posting #3 only the values matters
Is X a duplicate in this: a = {X:[]}, b = {X:[]}
No


Is y a duplicate in this: a = {X:[y]}, b={X:[[y]]}
No


Is y a duplicate in this: a = {X:['y', '1']}, b={X:['y', '0']}
Yes

i use this function to get the CHANGED values between two dicts, this works perfectly well...

# return a dict with all the changed values
def get_changed(a, b):
    filtered_dict = {}
    for key in a:
        try:
            a.get(key) != b[key]
        except KeyError:
            continue
        filtered_dict[key] = [value for value in b[key] if value not in a.get(key, [])]
    return filtered_dict
but this function that looksup the DELETED values returns wrong data, changed values are somehow returned as deleted values in addition to the actual deleted values, maybe i should only iterate the keys...no idea...

# return a dict that contains what is missing in current data
def get_deleted(a, b):
    answer = {}
    for key, value in a.items():
        try:
            v = b[key]
        except KeyError:
            continue
        ls_del = [i for i in value if i not in v]
        answer[key] = {}
        #answer[key] = ls_del or [None]
        answer[key] = ls_del
        print("get_delete_func", answer)
    return answer
Reply
#27
Quote:1
Is y a duplicate in this: a = {X:['y', '1']}, b={X:['y', '0']}
Yes
The entire list ['y', '1'], ['y', '0'], or just the 'y'? I thought the two value dictionaries represented key/values. From the original post:
['OS_TYPE', 'AIX']
Are OS_TYPE and AIX both values?
Reply
#28
[quote="deanhystad" pid='178745' dateline='1716824909']
Quote:Are OS_TYPE and AIX both values?

depends how you see it...from pythons point of view its a dict with subdicts where the subdicts contains lists with values. in another place in my code the lists values are split up and are used as key and value pairs, but this is not part of the actual topic. if the mentioned get_deleted function can be modified in a way to only catch the actual deleted items instead of also returning changed items as deleted this whole dedup circus can be ommited, alas i failed to do so.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 625 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Copying the order of another list with identical values gohanhango 7 1,366 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,435 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 1,419 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,998 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,232 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  remove partial duplicates from csv ledgreve 0 920 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Remove values for weekend in a panda series JaneTan 0 764 Dec-12-2022, 01:50 AM
Last Post: JaneTan
  Remove numbers from a list menator01 4 1,571 Nov-13-2022, 01:27 AM
Last Post: menator01
  Remove if similar values available based on two columns klllmmm 1 1,481 Feb-20-2022, 06:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020