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:
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.
Was about to type what Nick is saying . 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).
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.
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.
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
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