简体   繁体   中英

calculating the sum of digits in a list in python

I am new to math problems in python but basically i have the following code:

a = list(range(1,10000))
str(a)

sum_of_digits = sum(int(digit) for digit in str(a[9998]))
print(sum_of_digits)




this allows me to calculate the sum of the digits of a given number in the list a. but instead of feeding numbers into this one by one, i want an efficient way to calculate the sum of the digits of all the numbers in a and print them all out at once. I can't seem to figure out a solution but i know the answer is probably simple. any help is appreciated!

edit: i didnt know this post would get this much attention, for those wanting more clarification i basically want to know which digits in the list of range 1,9999 has a sum of 34 or more. i think everyone thought i simply wanted to take the sum of digits of each list element and then compile a total sum. in any case, that method helped me solve the actual problem

A good, straightforward way to do this is to use the modulo % operator, along with floor division \\\\ :

total_sum = 0
for num in a:
    sum_of_digits = 0
    while (num != 0):  
        sum_of_digits = sum_of_digits + (num % 10) 
        num = num//10
    total_sum = total_sum + sum_of_digits
print total_sum

Here, the expression n % 10 returns the remainder of dividing n by 10, or in other words, it returns the digit in the units place of that number. What the while loop is doing is repeatedly dividing the number by 10, then adding the number in the units place to the total.

Note that the \\\\ (floor division) is important here, as it gets rid of any decimal value in the number, which is needed for modulo % to work properly.

Note : This solution is massively more efficient than any algorithm which relies on str() .

Try this:

sum(int(i) for j in range(1,10000) for i in str(j))

It is the same, but works slowly:

lst = []
for j in range(10000):
    for i in str(j):
        lst.append(int(i))
print(sum(lst))

i want an efficient way to calculate the sum of the digits of all the numbers in a

If you truly want an efficient way, do not calculate the sum of the digit sum of all the individual numbers. Instead, calculate the total digit sum of the entire range 1 at once.

For example, in the range up to and including 123 , we do not have to write out all the individual numbers to see that the last digit will cycle through the numbers 1-9 a total of 12 times, plus the numbers 1-3 once. The middle digit cycles through 1-9 once, showing each 10 times, and then another 10 times 1 and 4 times 2. And for the first digit, only the 1 appears 24 times. Thus, the total is 45*12 + 1+2+3 + 45*10 + 10 + 8 + 24 = 1038 .

You can put this into a recursive formula using "a bit" of modulo magic.

def dsum(n, f=1, p=1):
    if n:
        d, r = divmod(n, 10)
        k = (45*d + sum(range(r)))*f + r*p
        return dsum(d, f*10, p + f*r) + k
    return 0

This yields the same results as the "naive" approach, but with a running time of O(log n) instead of O(n) it can be used to calculate the digit sum of ridiculously large ranges of numbers.

>>> n = 1234567
>>> sum(int(c) for i in range(1, n+1) for c in str(i))
32556016
>>> dsum(n)
32556016
>>> dsum(12345678901234567890)
1047782339654778234045

1) This is assuming your list is always a range of numbers starting at 1 up to some upper bound, although this would also work for a range not starting at 1 by calculating the digit sum for the upper bound and then subtracting the digit sum for the lower bound. If the list is not a range, then there's no way around calculating the digit sum for all the individual numbers, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM