Python Forum
Find Common Elements in 2 list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find Common Elements in 2 list
#1
Hello
I have this list:
A [(7.5,), (17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,)]
B [(17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,), (42.5,)]

I want to find number of same elements in the list
I tried these methods but the result is always zero:
list1_as_set = set(A)
intersection = list1_as_set.intersection(B)
intersection_as_list = list(intersection)
I also tried that one:
commonalities = set(A) - (set(A) - set(B))
But the result is empty ..
How can I find the common elements in 2 different list?
Reply
#2
Hope this helps
A = [(7.5,), (17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,)]
B = [(17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,), (42.5,)]

list(set(A).intersection(B))
Output:
[(37.5,), (40.0,), (30.0,), (27.5,), (17.5,), (25.0,), (22.5,)]
Reply
#3
>>> A = [(7.5,), (17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,)]
>>> B = [(17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,), (42.5,)]
>>> set(A).union(set(B))
{(37.5,), (40.0,), (30.0,), (27.5,), (17.5,), (7.5,), (42.5,), (25.0,), (22.5,)}
>>>
>>>
>>> # With lists better formatted:
>>> A = [7.5, 17.5, 22.5, 25.0, 27.5, 30.0, 37.5, 40.0]
>>> B = [17.5 22.5, 25.0, 27.5, 30.0, 37.5, 40.0, 42.5]
>>> set(A).union(set(B))
{37.5, 7.5, 40.0, 42.5, 17.5, 22.5, 25.0, 27.5, 30.0}
>>>
Reply
#4
(Apr-14-2021, 03:29 PM)klllmmm Wrote: Hope this helps
A = [(7.5,), (17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,)]
B = [(17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,), (42.5,)]

list(set(A).intersection(B))
Output:
[(37.5,), (40.0,), (30.0,), (27.5,), (17.5,), (25.0,), (22.5,)]

I have still empty list as a result ... Huh
Reply
#5
(Apr-14-2021, 03:29 PM)klllmmm Wrote: Hope this helps
A = [(7.5,), (17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,)]
B = [(17.5,), (22.5,), (25.0,), (27.5,), (30.0,), (37.5,), (40.0,), (42.5,)]

list(set(A).intersection(B))
Output:
[(37.5,), (40.0,), (30.0,), (27.5,), (17.5,), (25.0,), (22.5,)]
It is ok now
THanks!
klllmmm likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Checking if a string contains all or any elements of a list k1llcod3 1 242 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 491 Nov-15-2022, 08:40 PM
Last Post: tester_V
  How to change the datatype of list elements? mHosseinDS86 9 914 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  read a text file, find all integers, append to list oldtrafford 12 1,506 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  find some word in text list file and a bit change to them RolanRoll 3 764 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to find the second lowest element in the list? Anonymous 3 759 May-31-2022, 01:58 PM
Last Post: Larz60+
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 3,071 May-17-2022, 11:38 AM
Last Post: Larz60+
  Python Program to Find the Total Sum of a Nested List vlearner 8 2,581 Jan-23-2022, 07:20 PM
Last Post: menator01
  Find the highest value of a list Menthix 4 1,260 Oct-29-2021, 02:32 PM
Last Post: Menthix
  Why am I getting list elements < 0 ? Mark17 8 2,173 Aug-26-2021, 09:31 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

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