def count_digit(num):
if (num//10 == 0):
return 1
else:
return 1 count_digit(num // 10)
i have a list called - filter_stats which looks like :
filter_stats= [1,2,24,2,353,4534,6,65,6457,6,8,58,58,744, and so on]
I want to pass this list in the code above. how do I do that?
when I did this
count_digit(filter_stats)
it shows this: unsupported operand type(s) for //: 'filter' and 'int'
CodePudding user response:
You can use a for loop:
results = []
for num in filter_stats:
results.append(count_digit(num))
A list comprehension is also an option:
results = [count_digit(num) for num in filter_stats]
Finally, you can also use map
if you like functional approaches:
results = list(map(count_digit, filter_stats))
CodePudding user response:
You are passing a list into the count_digit function. So when you divide a list by "// 10", that would give an error. To see the length of a number, you can use the len() function, so e.g. len(str(123)).
So, in your code you have to pass in the list item : e.g: count_digit(filter_stats[4]) => then it works!
you can simplify the function like this:
def count_digit(num):
return len(str(num))
and then call the function :
count_digit(filter_stats[4])