Python Forum
checking for matches between lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
checking for matches between lists
#1
Hey guys

so here's the problem
I have 2 lists:
numbers = [1,2,3,4,5,6,7,8,9]
given = [1,1,2]
how do i find the amount of elements in the given-list that match the numbers-list?

len(set(numbers) & set(given)) gives me 2, because it doesn't count the multiple ones
how do i also count the multiple matches, giving me 3?

(the real question is with letters but the numbers are to simplify things)


thank you!
Reply
#2
Iterate through the elements in given checking if each entry is in the numbers list.
Use a for loop and the in operator.
Reply
#3
(Oct-24-2017, 10:46 AM)Mekire Wrote: Iterate through the elements in given checking if each entry is in the numbers list.
Use a for loop and the in operator.

aah very much appreciated for your quick and honest help!

kind regards
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  delete a Python object that matches an atribute portafreak 2 2,134 Feb-19-2020, 12:48 PM
Last Post: portafreak
  Checking 2 lists graham23s 2 2,066 Sep-01-2019, 01:41 AM
Last Post: DeaD_EyE
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,187 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Detecting if image matches another kainev 2 2,864 Dec-02-2018, 02:00 PM
Last Post: kainev
  How to detect and tell user that no matches were found in a list RedSkeleton007 6 3,810 Jul-19-2018, 06:27 PM
Last Post: woooee
  for any loop _ want to know which matches 3Pinter 8 4,296 Jun-14-2018, 10:04 AM
Last Post: buran
  Check to see if a string exactly matches an element in a list DBS 1 22,155 Nov-02-2016, 10:16 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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