Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to compare several unordered list and get the list names if there are matching values

I have several lists containing values. just like below.

Alc=['P-111127759','111157751','1123104714']
FItems=['1123104714','797917266','79791761','79791765','79791763']
kuVItem=['1110234713','161231437','756623557','1123104714','7630672177', '754955924','712969245','963176673','181104711']
Products=['7171523315','Pqw-805967111','1150596117','115596116']
WProducts=['PQ-4559678','7171523315','5505296117','755961146']

I basically want to find out the list names if there are any values similar in lists.
For example, Alc, FItems, and kuVItem lists have the same value, "1123104714". So I basically want those names as pairs. I mean like this

['Alc', 'FItems'],['Alc', 'kuVItem'],['FItems','kuVItem']

Is there a way to do this in python?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance! Any help is appreciated!

>Solution :

I assumed that the values are gonna be supplied through a dictionary(which is the only way I can think of for named values whose name change often).

Then just iterate through the keys, and for each key, compare the value with other entries. Since we just want to know if there is overlap, set.intersection() is a great tool for this.

My code ended up being:

my_dict = {
  'Alc':['P-111127759','111157751','1123104714'],
  'FItems':['1123104714','797917266','79791761','79791765','79791763'],
  'kuVItem':['1110234713','161231437','756623557','1123104714','7630672177', '754955924','712969245','963176673','181104711'],
  'Products':['7171523315','Pqw-805967111','1150596117','115596116'],
  'WProducts':['PQ-4559678','7171523315','5505296117','755961146']}


results = []
for key in my_dict:
  for key2 in my_dict:
    if key != key2:
      intersection = set.intersection(set(my_dict[key]), set(my_dict[key2]))
      if len(intersection) > 0:
        if [key2, key] not in results and [key, key2] not in results:
          results.append([key, key2])

print(results)

Which outputs [['Alc', 'FItems'], ['Alc', 'kuVItem'], ['FItems', 'kuVItem'], ['Products', 'WProducts']]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading