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
  Why am I getting list elements < 0 ? Mark17 8 483 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 264 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 390 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Make Groups with the List Elements quest 2 387 Jul-11-2021, 09:58 AM
Last Post: perfringo
  I cannot delete and the elements from the list quest 4 818 May-11-2021, 12:01 PM
Last Post: perfringo
  List of lists - merge sublists with common elements medatib531 1 801 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sorting Elements via parameters pointing to those elements. rpalmer 3 789 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Group List Elements according to the Input with the order of binary combination quest_ 19 1,732 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  List of error codes to find (and count) in all files in a directory tester_V 8 1,080 Dec-11-2020, 07:07 PM
Last Post: tester_V
  Find _ in a list 1234 15 1,620 Dec-01-2020, 07:53 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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