Python – Average digits count in a List
Last Updated :
19 Mar, 2023
Given a list of elements extract the average digit count in List.
Input : test_list = [34, 2345, 23, 456, 2, 23, 456787]
Output : 2.857142857142857
Explanation : Average of all digit count. [2+4+2+3+1+2+6 = 20, 20/7 = 2.857142857142857]
Input : test_list = [34, 1, 456]
Output : 2.0
Explanation : Average of all digit count. [1 + 2 + 3 = 6, 6 / 3 = 2]
Method #1 : Using len() + loop + str()
In this, we iterate each element, convert to string, and find its length, perform summation using counter, and then divide the result with the total element in the list to get results.
Python3
test_list = [ 34 , 2345 , 23 , 456 , 2 , 23 , 456787 ]
print ( "The original list is : " + str (test_list))
sumd = 0
for ele in test_list:
sumd + = len ( str (ele))
res = sumd / len (test_list)
print ( "Average digits length : " + str (res))
|
Output
The original list is : [34, 2345, 23, 456, 2, 23, 456787]
Average digits length : 2.857142857142857
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using len() + sum() + str()
In this, the task of getting a sum is done using sum(), extends a compact way to solve the problem.
Python3
test_list = [ 34 , 2345 , 23 , 456 , 2 , 23 , 456787 ]
print ( "The original list is : " + str (test_list))
res = sum ([ len ( str (ele)) for ele in test_list]) / len (test_list)
print ( "Average digits length " + str (res))
|
Output
The original list is : [34, 2345, 23, 456, 2, 23, 456787]
Average digits length 2.857142857142857
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.
Method #3 : Using the mean() function from the statistics module :
Algorithm:
1.Initialize a list to hold the lengths of digits for each element in the input list.
2.Iterate over the elements in the input list and convert each element to string and find the length of the string. Append this length to the list from step 1.
3.Calculate the average of the list from step 1 using the mean() function from the statistics module.
4.Return the average digits count.
Python3
import statistics
test_list = [ 34 , 2345 , 23 , 456 , 2 , 23 , 456787 ]
print ( "The original list is : " + str (test_list))
digits_lengths = [ len ( str (num)) for num in test_list]
average_digits = statistics.mean(digits_lengths)
print ( "Average digits count:" , average_digits)
|
Output
The original list is : [34, 2345, 23, 456, 2, 23, 456787]
Average digits count: 2.857142857142857
Time Complexity: O(n), where n is the length of the input list. The for loop that iterates over the elements in the input list has a time complexity of O(n) and finding the length of a string has a time complexity of O(1).
Auxiliary Space: O(n), where n is the length of the input list. We need to store the length of each element’s digits in a separate list, which has a space complexity of O(n).
Method #4: Using list comprehension and the sum() function:
Initialize the list of integers.
Use list comprehension to get the length of each integer’s digits and create a new list of those lengths.
Use the sum() function to get the total length of all the digits.
Divide the total length by the number of integers in the list to get the average length.
Print the result.
Python3
test_list = [ 34 , 2345 , 23 , 456 , 2 , 23 , 456787 ]
print ( "The original list is : " + str (test_list))
lengths = [ len ( str (ele)) for ele in test_list]
total_length = sum (lengths)
avg_length = total_length / len (test_list)
print ( "Average digits length : " + str (avg_length))
|
Output
The original list is : [34, 2345, 23, 456, 2, 23, 456787]
Average digits length : 2.857142857142857
Time complexity: O(n), where n is the length of the input list
Auxiliary space: O(n), where n is the length of the input list
Share your thoughts in the comments
Please Login to comment...