0

I have two lists with some items in common and some not. I would like to compare the two lists and get the count of items that matched.

list1 = ['apple','orange','mango','cherry','banana','kiwi','tomato','avocado']
list2 = ['orange','avocado','kiwi','mango','grape','lemon','tomato']

Pls advice how to do this in python

Sudhi
  • 421
  • 1
  • 8
  • 19

2 Answers2

1

Use Counters and dictionary comprehension.

list1 = ['apple','orange','mango','cherry','banana','kiwi','tomato','avocado']
list2 = ['orange','avocado','kiwi','mango','grape','lemon','tomato']
c1 = Counter(list1)
c2 = Counter(list2)
matching = {k: c1[k]+c2[k] for k in c1.keys() if k in c2}
print(matching)
print('{} items were in both lists'.format(len(macthing))

Output:

{'avocado': 2, 'orange': 2, 'tomato': 2, 'mango': 2, 'kiwi': 2}
5 items were in both lists
PyPingu
  • 1,697
  • 1
  • 8
  • 21
  • Thank you. I shall test this in my code and get back – Sudhi Oct 04 '18 at 14:55
  • Could you please advice if I have multiple such lists and I just want to compare one pair of list at a time. Ex: List1 and List2 only, then List3 and List4 only. Lets say the lists in the dataframe appear List1 and List3 in one column and List2 and List4 in the column beside. – Sudhi Oct 04 '18 at 17:58
  • It's hard to answer that without seeing a proper example of your data. Sounds like you will want to loop through the pairs though. – PyPingu Oct 04 '18 at 21:25
  • df = pd.DataFrame({ 'Col1':[['Phone', 'Watch', 'Pen', 'Pencil', 'Knife'],['apple','orange','mango','cherry','banana','kiwi','tomato','avocado']], 'Col2': [['Phone', 'Watch', 'Pen', 'Pencil', 'fork'],['orange','avocado','kiwi','mango','grape','lemon','tomato']]}) – Sudhi Oct 05 '18 at 04:52
  • I have given an example df code. You will notice that the columns in row one is only worth comparing and similarly the columns in row two can be compared. I would like to do this as well. – Sudhi Oct 05 '18 at 04:53
  • No worries. I figured out the answer myself. We just need to write a for loop with the below syntax: `for index1,(lst1,lst2) in enumerate(zip(df['Col1'],df['Col2']))` – Sudhi Oct 05 '18 at 05:47
1

I think you can use set.intersection within a comprehension like this example:

list1 = ['apple','orange','mango','cherry','banana','kiwi','tomato','avocado']
list2 = ['orange','avocado','kiwi','mango','grape','lemon','tomato']

result = {elm: list1.count(elm) + list2.count(elm) for elm in set.intersection(set(list1), set(list2))}

Output:

{'kiwi': 2, 'avocado': 2, 'orange': 2, 'tomato': 2, 'mango': 2}
Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43