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
# Python3 code to demonstrate working of # Average digits count # Using len() + loop + str() # initializing list test_list = [ 34 , 2345 , 23 , 456 , 2 , 23 , 456787 ] # printing original list print ( "The original list is : " + str (test_list)) sumd = 0 for ele in test_list: # summing digits length sumd + = len ( str (ele)) # getting result after dividing total digits by elements res = sumd / len (test_list) # printing result print ( "Average digits length : " + str (res)) |
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
# Python3 code to demonstrate working of # Average digits count # Using len() + sum() + str() # initializing list test_list = [ 34 , 2345 , 23 , 456 , 2 , 23 , 456787 ] # printing original list print ( "The original list is : " + str (test_list)) # getting summation and dividing by total length res = sum ([ len ( str (ele)) for ele in test_list]) / len (test_list) # printing result print ( "Average digits length " + str (res)) |
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 the statistics module import statistics # initializing list test_list = [ 34 , 2345 , 23 , 456 , 2 , 23 , 456787 ] # printing original list print ( "The original list is : " + str (test_list)) # calculate the average digits count using the mean() function digits_lengths = [ len ( str (num)) for num in test_list] average_digits = statistics.mean(digits_lengths) # print the result print ( "Average digits count:" , average_digits) #This code is contributed by Jyothi pinjala. |
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
# Python3 code to demonstrate working of # Average digits count # Using list comprehension and sum() # initializing list test_list = [ 34 , 2345 , 23 , 456 , 2 , 23 , 456787 ] # printing original list print ( "The original list is : " + str (test_list)) # using list comprehension to get lengths of digits lengths = [ len ( str (ele)) for ele in test_list] # summing the lengths to get total length total_length = sum (lengths) # calculating average avg_length = total_length / len (test_list) # printing result print ( "Average digits length : " + str (avg_length)) |
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