7

In resume, I have two keys in the same dictionary where each one has their corresponding lists.

I try to compare both list to check common and differential elements. It means that the output I will count how many elements are identical or present in only one key's list.

from the beginning I am inserting the elements using the files as arguments and they are read in the function

def shared(list):
        dict_shared = {}
        for i in list:
                infile = open(i, 'r')
                if i not in dict_shared:
                        dict_shared[i] = []

                for line in infile:
                        dict_shared[spacer].append(record.id)

        return dict_shared

Now I am stuck trying to find a way to compare the lists created and present in the dictionary.

dict = {a:[1,2,3,4,5], b:[2,3,4,6]}

My intention is to compare the lists in order to have the lines shared between two texts.

a: [1,5]
b: [6]
a-b: [2,3,4]

From now I can't find a way to solve this. Any suggestion?

F.Lira
  • 663
  • 2
  • 6
  • 19

6 Answers6

5

You could use set:

d = {'a':[1,2,3,4,5], 'b':[2,3,4,6]}
print(list(set(d['a'])-set(d['b'])))
print(list(set(d['b'])-set(d['a'])))
print(list(set(d['b'])&set(d['a'])))

result:

[1, 5]
[6]
[2, 3, 4]
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
5

you can do that by utilising python inbuilt functions like union, difference, intersection. Note: These are for sets, you can convert a list to set by

1stset = set(a)

example:

print(1stset.difference(2ndset))

print(1stset.intersection(2ndset))

print(1stset.union(2ndset))

you can refer the following links for more information

https://www.geeksforgeeks.org/python-intersection-two-lists/

https://www.geeksforgeeks.org/python-union-two-lists/

https://www.geeksforgeeks.org/python-difference-two-lists/

mohammed wazeem
  • 1,310
  • 1
  • 10
  • 26
Lavanya V
  • 337
  • 2
  • 7
  • it is consider better to provide solution or stackoverflow link than other website link for other benefits – sahasrara62 Apr 16 '20 at 13:39
  • I saw it before to come here but in my case I have the lists in one dictionary, so I was trying to do it in some recursive way. – F.Lira Apr 16 '20 at 13:41
4

A solution with list comprehension would be:

dictionary = {'a':[1,2,3,4,5], 'b':[2,3,4,6]}

only_in_a = [x for x in dictionary['a'] if not x in dictionary['b']]
only_in_b = [x for x in dictionary['b'] if not x in dictionary['a']]
in_both = [x for x in dictionary['a'] if x in dictionary['b']]

Note that this is not especially wise in terms of complexity, for larger lists.

johannesack
  • 660
  • 3
  • 19
3

Not sure if I understand correctly what you are trying to achieve but it seems like you'd need set operations:

dictionary = {"a":[1,2,3,4,5], "b":[2,3,4,6]}

#in a but not in b
set(dictionary["a"]) - set(dictionary["b"])

#in b but not in a
set(dictionary["b"]) - set(dictionary["a"])

#union of both
set(dictionary["b"]).union(set(dictionary["a"]))

#intersection of both
set(dictionary["b"]).intersection(set(dictionary["a"]))
kjul
  • 156
  • 6
3

You can try something like this

mydict = {'a': [1,2,3,4,5], 'b': [2,3,4,6]}

>>> list(set(mydict['a']).intersection(mydict['b']))  # common to both
 [2, 3, 4]
>>> list(set(mydict['a']).difference(mydict['b']))  # a - b
 [1, 5]
>>> list(set(mydict['b']).difference(mydict['a']))  # b - a
 [6]
>>> list(set(mydict['a']).union(mydict['b']))  # union of both
 [1, 2, 3, 4, 5, 6]
mohammed wazeem
  • 1,310
  • 1
  • 10
  • 26
2

Try this

print("a - b : {} ".format(list(set(_dict['a']) - set(_dict['b']))))
print('b - a : {} '.format(list(set(_dict['b']) - set(_dict['a']))))
print('a \u2229 b : {} '.format(list(set(_dict['a']).intersection(set(_dict['b'])))))

Output

a - b : [1, 5] 
b - a : [6] 
a ∩ b : [2, 3, 4]
FAHAD SIDDIQUI
  • 631
  • 4
  • 22