0
Count ([[[[[[2]]]]]],[[[[2]]]],2)
How do i implement a function to count number of 2
2 ответов
+ 5
You can use flattening, i have done code for this some months ago.
https://code.sololearn.com/cUgNFWGrImML/?ref=app
+ 4
If you don't know how many nested levels your list might have, recursion is always a good option.
Set the count to 0
Loop through each element in the list
If the element is a number, add it to the count
If its a list, do the recursion
Finally return the count.
One technique to determine if something is a list or not, is duck typing. Try to iterate, if you get an error then it is not iterable.