Python Forum
How to sum up the elements of an integer using python split/join?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to sum up the elements of an integer using python split/join?
#1
I am trying to sum up the elements of an integer using python split join
Here is my code,
-----------------------
a='1234'
b=list(a)
c='+'.join(b)
print© ##output- 1+2+3+4
-------------------------------------
How to get the sum of all the elements
Reply
#2
Do you want "sum up the elements of an integer" or "sum up the elements of an integer using python split join"?

What is expected outcome? 1 + 2 + 3 + 4 = 10 or 1 + 2 + 3 + 4 = 10 --> 1 + 0 = 1
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Thanks for you reply.
i want the output like this 1+2+3+4= 10
Reply
#4
Still, output should be '1 + 2 + 3 + 4 = 10' or just '10'?

You can have this way:

>>> a = '1234' 
>>> b = list(a)
>>> total = sum([int(x) for x in b]) 
>>> f"{' + '.join(b)} = {total}"
'1 + 2 + 3 + 4 = 10' 
Or just sum:

>>> a = '1234'
>>> sum([int(x) for x in list(a)])
10
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Thanks a lot perfringo!!
Reply
#6
You are welcome!

Some observations and for future references.

It is very important to have correct name for thread. Currently it reads "How to sum up the elements of an integer using python split/join?" and there are several problems with that: there is no elements in integer as integer is not iterable; integer does not support split/join. Problem is actually about string which looks like integer.

If somebody searches for solution how sum up digits in integer and reaches this thread will be disappointed that despite promising name it's about something else (of course, one can always do str(1234) and use code above).

For those, who looking for "how to sum up digits in integer" without converting into string:

1. Only digits which are at display (shallow sum) i.e 149 = 1 + 4 + 9 = 14. This solution relies of Python feature that in truth value testing zero of any numeric type is false

def sum_digits(i):
    total = 0
    while i:
        total += i % 10
        i //= 10
    return total
2. Sum integer digits until there is only one digit (deep sum), i.e 149 --> 1 + 4 + 9 = 14 --> 1 + 4 = 5 This relies on math concept of modulo 9 and not any specific Python feature:

def sum_digits(i):
    return (i - 1) % 9 + 1
If modulo 9 concept seems foreign one can use shallow sum recursively to have deep sum:

def sum_digits(i):
    total = 0
    while i:
        total += i % 10
        i //= 10
    if total < 10: 
        return total
    else:
        return sum_digits(total)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove some elements from an array in python? gohanhango 9 986 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,013 May-17-2022, 11:38 AM
Last Post: Larz60+
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 2,490 May-24-2021, 08:48 AM
Last Post: bowlofred
  Testing Split elements tester_V 4 2,401 May-08-2021, 10:54 PM
Last Post: snippsat
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,550 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  The difference between os.path.join( and os.sep.join( Pedroski55 2 9,310 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  How does a set in python store the elements? idknuttin 5 2,704 Jul-10-2020, 10:46 PM
Last Post: Gribouillis
  Join table with differnt datype column using python query abhishek6555 1 2,260 Jan-07-2020, 11:44 PM
Last Post: micseydel
  Beginner at Python. Trying to count certain integer from random string of code kiaspelleditwrong 3 2,367 Oct-14-2019, 10:40 AM
Last Post: perfringo
  python cache for small integer Uchikago 1 2,457 Jun-27-2019, 05:32 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020