{"id":12421,"date":"2021-10-01T10:00:51","date_gmt":"2021-10-01T04:30:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=12421"},"modified":"2021-11-22T18:33:30","modified_gmt":"2021-11-22T13:03:30","slug":"python-program-to-calculate-the-average-of-a-numbers-digits-of-every-number-in-given-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-calculate-the-average-of-a-numbers-digits-of-every-number-in-given-list\/","title":{"rendered":"Python Program to Calculate the Average of a Number\u2019s digits of Every Number in Given List"},"content":{"rendered":"
Given a list, the task is to calculate the average of every number digit in the given List in Python.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to Calculate the Average of a Number\u2019s digits of Every Number in the given List in Python<\/p>\n Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output<\/a><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Given a list, the task is to calculate the average of every number digit in the given List in Python. Examples: Example1: Input: Given list =[9, 15, 21, 356, 3243, 9139, 4467285, 123456, 5783892, 4535363, 69] Output: The list before calculating Average of digits = [9, 15, 21, 356, 3243, 9139, 4467285, 123456, 5783892, 4535363, …<\/p>\nGiven list =[9, 15, 21, 356, 3243, 9139, 4467285, 123456, 5783892, 4535363, 69]<\/pre>\n
The list before calculating Average of digits = [9, 15, 21, 356, 3243, 9139, 4467285, 123456, 5783892, 4535363, 69]\r\nThe list after calculating Average of digits = [9.0, 3.0, 1.5, 4.666666666666667, 3.0, 5.5, 5.142857142857143, 3.5, 6.0, 4.142857142857<\/pre>\n
Given list = [245, 1739, 102, 199, 488, 58]<\/pre>\n
The list before calculating Average of digits = [245, 1739, 102, 199, 488, 58]\r\nThe list after calculating Average of digits = [3.6666666666666665, 5.0, 1.0, 6.333333333333333, 6.666666666666667, 6.5]<\/pre>\n
Program to Calculate the Average of a Number\u2019s digits of Every Number in Given List in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function numAverageDigits() which accepts the number as an argument and\r\n# returns the Average of all the digits in the given Number.\r\n\r\n\r\ndef numAverageDigits(numbe):\r\n # convert the given number to string using str() function\r\n stringnum = str(numbe)\r\n # Create a list of digits using map(),list(),int functions\r\n digitslist = list(map(int, stringnum))\r\n # calculate the sum of digits of the above digitslist\r\n sumdigits = sum(digitslist)\r\n # calculate the total number of digits by calculating length of the digits list\r\n numOfDigits = len(digitslist)\r\n # calculate the average of digits of the given number\r\n # by diving sum of digits by number of digits\r\n avgdigits = sumdigits\/numOfDigits\r\n # Return the average of the digits.\r\n return avgdigits\r\n\r\n\r\n# Give the list as static input and store it in a variable.\r\ngivenlist = [9, 15, 21, 356, 3243, 9139, 4467285, 123456, 5783892, 4535363, 69]\r\n# Take an empty to store the Average digit sums of all the individual elements\r\n# using [] or list() say resulist.\r\nresulist = []\r\n# Traverse the given list using For loop.\r\nfor Number in givenlist:\r\n # Pass the iterator value(element) to the numAverageDigits() as an argument\r\n # and append the result to the resulist using the append() function.\r\n resulist.append(numAverageDigits(Number))\r\n# Print the givenlist before calculating Average of digits\r\nprint('The list before calculating Average of digits = ', givenlist)\r\n# Print the resulist after calculating Average of digits\r\nprint('The list after calculating Average of digits = ', resulist)\r\n<\/pre>\n
The list before calculating Average of digits = [9, 15, 21, 356, 3243, 9139, 4467285, 123456, 5783892, 4535363, 69]\r\nThe list after calculating Average of digits = [9.0, 3.0, 1.5, 4.666666666666667, 3.0, 5.5, 5.142857142857143, 3.5, 6.0, 4.142857142857143, 7.5]<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Create a function numAverageDigits() which accepts the number as an argument and\r\n# returns the Average of all the digits in the given Number.\r\n\r\n\r\ndef numAverageDigits(numbe):\r\n # convert the given number to string using str() function\r\n stringnum = str(numbe)\r\n # Create a list of digits using map(),list(),int functions\r\n digitslist = list(map(int, stringnum))\r\n # calculate the sum of digits of the above digitslist\r\n sumdigits = sum(digitslist)\r\n # calculate the total number of digits by calculating length of the digits list\r\n numOfDigits = len(digitslist)\r\n # calculate the average of digits of the given number\r\n # by diving sum of digits by number of digits\r\n avgdigits = sumdigits\/numOfDigits\r\n # Return the average of the digits.\r\n return avgdigits\r\n\r\n\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# store it in a variable.\r\ngivenlist = list(\r\n map(int, input('Enter some random List Elements separated by spaces = ').split()))\r\n# Take an empty to store the Average digit sums of all the individual elements\r\n# using [] or list() say resulist.\r\nresulist = []\r\n# Traverse the given list using For loop.\r\nfor Number in givenlist:\r\n # Pass the iterator value(element) to the numAverageDigits() as an argument\r\n # and append the result to the resulist using the append() function.\r\n resulist.append(numAverageDigits(Number))\r\n# Print the givenlist before calculating Average of digits\r\nprint('The list before calculating Average of digits = ', givenlist)\r\n# Print the resulist after calculating Average of digits\r\nprint('The list after calculating Average of digits = ', resulist)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 245 1739 102 199 488 58\r\nThe list before calculating Average of digits = [245, 1739, 102, 199, 488, 58]\r\nThe list after calculating Average of digits = [3.6666666666666665, 5.0, 1.0, 6.333333333333333, 6.666666666666667, 6.5]<\/pre>\n
\n