Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic Way to compare two unordered lists by attributes

Tags:

python

list

what is the most pythonic way to compare two unordered lists by one or more of their attributes? I would love to know if there is a pythonic way to find out if for each item in a list A there exists an item in list B where the item from list A and the item in list B match in a specified attribute.

In my example case, I have two .zip files in a unit test, and want to test, if the files match, but I am really looking for a good general solution for my personal toolset. This was my first attempt:

with ZipFile('A.zip') as old:
with ZipFile('B.zip') as new:
oldFileInfo = old.infolist()

allFound = True
for info in new.infolist():
   matches = [item for item in oldFileInfo if item.CRC == info.CRC and \   
              basename(item.filename) == basename(info.filename) ]
   if len(matches) == 0:
       allFound = False
       break

Maybe it is trivial, but I have not yet found a nice way how to do it.

Greetings Michael

like image 400
Michael Avatar asked Sep 11 '26 08:09

Michael


2 Answers

It is easy, you should use sets:

if set(list1).difference(set(list2)):
    # lists are different
    # different_items = set(list1).difference(set(list2))
    pass
else:
    # lists are the same
    pass

You can convert your structure to iterables or lists:

list1 = [(i.CRC, basename(i.filename)) for i in old.infolist()]
list2 = [(i.CRC, basename(i.filename)) for i in new.infolist()]
like image 103
Jiri Avatar answered Sep 13 '26 20:09

Jiri


One possible way to do it can be:

def areEqual(old, new):
    set1 = set((x.attribute1, x.attribute2) for x in old)
    set2 = set((x.attribute1, x.attribute2) for x in new)

    return set1 == set2
like image 31
Sunny Nanda Avatar answered Sep 13 '26 20:09

Sunny Nanda