Counting occurences of each element in a list in another list

Hello

as the title suggests, i have to create a script that will check how many times an instance in a list is repeated in a min list.
I tried “CountOccurarences” Node, but it doesnt work:

for example 4 is listed 3 timesand its showing 0

could you please help me out, or point me in the right direction

1 Like

@NERDASORUS ,

try this


KR
Andreas

4 Likes

Hello,
Here is an approach
edit2:

Cordially
christian.stan

1 Like

Make sure you’re reading the description of a node and not just basing your use off the name. String.CountOccurrences counts the number of times a substring exists within a string. It’s not counting the objects in a list.

You just need a combination of grouping and filtering your items so you get get their counts accurately.

3 Likes

a solution with python

3 Likes

HI @NERDASORUS ,

Was about to type what Nick is saying :slight_smile: . In addition to that also note that your approach with changing numbers to strings is flawed, since the string “10” itself also contains “1”, and would thus always result in false positives. (@christian.stan I don’t think your first approach would work with “10” and “1” simultaneously).

See if this works for you:

3 Likes

I understood with 111 too
thanks to you
remains the solution with list set
but more efficient the python solution
cordially
christian.stan

double down on what @c.poupin said, I have found this kind of stuff much simpler and faster with some basic python. He showed it with the kind of loop that twists my brain up, but basically you want to do a forloop that will iterate through two lists. I like to do it kind of like this:


list1 = IN[0]
list2 = IN[1]
myitems = []
mycount = 0

for m in list1:
    for n in list2:    
        if m==n:
            mycount += 1  
            myitems.append(n)
        else: 
        pass

OUT = mycount, myitems

Im sure this code would throw errors, but its the gist of it. Nested a foreloop inside a forloop creates something like cross product lacing essentially, it compares every item to every item. For each occurance where the placeholder “m” from list 1 equals the placeholder “n” from list 2, it adds + 1 to the list (the += basically is shorthand for x=x+1, so you could also say mycount=mycount+1)

then your outlist will give you a total after python iterates through both lists. I will frequently also kick out the items themselves in another list using list.append() because thats often what I need to work with.

Hello, if I understand correctly a loop in a tuple is not a tuple, but in this case the class generator is it a list class?

import sys
a=[IN[0].count(i) for i in IN[1]]
b=(IN[0].count(i) for i in IN[1])
c=(1,2,3)

OUT = type(a),a,type(b),b,type(c),c

Cordially
christian.stan

1 Like

He used a dictionary though, altough I prefer to just use zip() myself when working with multiple lists just for the simplicity of it, although its probably not as fast as a dictionary for recall if working with huge data sets.

2 Likes

I used a dictionary comprehension (like list comprehension)

the synthax

dict_square = {n: n**2 for n in range(5)}
print(dict_square)

is equivalent to

dict_square = {}
for n in range(5):
	dict_square[n] = n**2
print(dict_square)
2 Likes

Thank you I understood that (dictionary and writing list comprehension) I consult a lot of your contributions (I progress step by step)
my question was more about the generator class
cordially
christian.stan

Here are some features of lists and generators

  • both types are iterable
  • generators don’t support indexing or slicing ( the generator is not subscriptable .)
  • generators can’t be added to lists
  • generators → less memory

no :grinning:

image

1 Like

Thank you for answering me and sorry for the digression (I have to force myself to avoid this) in relation to the initial subject
cordially
christian.stan

1 Like