Duke Kim
Duke Kim

Reputation: 61

Using Python split function to separate and add data

I have used split function to split my hourly data by every 12 zeros and not sure how to

add all the numbers in each string together can anyone help me? Thanks

x = '00000000000034305023243243000000000000023432432432000100053030900000000000'
x.split('0'*12)

Output: ['', '34305023243243', '023432432432000100053030900000000000']

So here I'm trying to add 34305023243243 together and 023432432432000100053030900000000000 together. I do know that I have to split the string again to get the individual numbers but using split() function gives me 'list' object has no attribute 'split'...

Upvotes: 0

Views: 108

Answers (1)

Sean
Sean

Reputation: 552

You can convert each string to a list, map the values to integers, and then sum up the sublists.

y = [sum(map(int, list(i))) for i in x]

Gives you a list of the sums

Upvotes: 1

Related Questions