user16414300
user16414300

Reputation:

Python sum only number in list

I want to sum only number in list , my list contain also characters what is best way? I try this

x = ['0.1₾', '0.1₾', '2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '5₾', '2₾', '9.9₾', '0.7₾', '5₾', '0.9₾', '0.8₾', '0.2₾', '1₾', '0.2₾', '3₾', '0.3₾', '5₾', '0.1₾', '2₾', '0.8₾', '1₾']
y = 0
for i in x:
    y += float(i)
print(y)

but error:

ValueError: could not convert string to float: '0.1₾'

what is best solution ?`

Upvotes: 0

Views: 150

Answers (8)

Kenzzz
Kenzzz

Reputation: 85

First try to find the printable representation for special character using: print repr(x[0])

Just need to add code to replace the special character:

x = ['0.1₾', '0.1₾', '2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '5₾', '2₾', '9.9₾', '0.7₾', '5₾', '0.9₾', '0.8₾', '0.2₾', '1₾', '0.2₾', '3₾', '0.3₾', '5₾', '0.1₾', '2₾', '0.8₾', '1₾']
y = 0
for i in x:
    a = i.replace('₾','')
    y += float(a)

print(y)
>>> print(y)
41.3

Upvotes: 0

Matiiss
Matiiss

Reputation: 6156

Here is my take on the problem:

x = ['0.1₾', '0.1₾', '2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '5₾', '2₾', '9.9₾', '0.7₾', '5₾', '0.9₾', '0.8₾', '0.2₾', '1₾', '0.2₾', '3₾', '0.3₾', '5₾', '0.1₾', '2₾', '0.8₾', '1₾']
print(sum([float(a.replace('₾', '')) for a in x]))

it is pretty short, but it works, basically it prints the sum of a list that is made up of float values from a string in which '₾' is basically removed by leaving just the number and so for each item in x, in short: a simple list comprehension.

could also use any of the following inside the float() (in this case at least):

a.split('₾')[0]
a[0:-1]  # a[:-1]

Upvotes: 0

A.M. Ducu
A.M. Ducu

Reputation: 900

Here's a short solution using list comprehension:

total = sum([float(f.replace('₾', '')) for f in x])
# or the variant using string slices
total = sum([float(f[:-1]) for f in x])
# we only convert the string without the last character

Where x is your list and total is the sum of all float values.

Upvotes: 1

Aniketh Malyala
Aniketh Malyala

Reputation: 2670

Since the character always appears as the last character in every element in the array, you can use the substring method to isolate the float part of each element

All you would need to change in your code is instead of:

y += float(i)

use:

y += float((i[0:len(i) - 1]))

If you want to use negative indexing:

y += float((i[0:-1]))

Either of the options above work :)

Upvotes: 0

imxitiz
imxitiz

Reputation: 3987

You can simply do this:

x = ['0.1₾', '0.1₾', '2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '5₾', '2₾', '9.9₾', '0.7₾', '5₾', '0.9₾', '0.8₾', '0.2₾', '1₾', '0.2₾', '3₾', '0.3₾', '5₾', '0.1₾', '2₾', '0.8₾', '1₾']
print(sum(map(lambda y:float(y.replace("₾","")),x)))
# 41.3

We are just replacing the to empty and changing string to float and doing sum using sum function!

For more information about sum function you can visit here and for map here.

Upvotes: 1

Corralien
Corralien

Reputation: 120479

Use regular expression to extract simple float and int numbers:

# import re
>>> sum([float(re.findall(r'([+-]?(?:[0-9]*[.])?[0-9]+)', s)[0]) for s in x])
41.3

Upvotes: 1

Ashish Acharya
Ashish Acharya

Reputation: 3399

Fairly brittle, but works fine:

x = ['0.1₾', '0.1₾', '2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '0.2₾', '5₾', '2₾', '9.9₾', '0.7₾', '5₾', '0.9₾', '0.8₾', '0.2₾', '1₾', '0.2₾', '3₾', '0.3₾', '5₾', '0.1₾', '2₾', '0.8₾', '1₾']
y = 0
for i in x:
    y += float(i.split('₾')[0])
print(y)

Upvotes: 1

Adrien
Adrien

Reputation: 433

You have to get rid of the ₾ symbol.

This will work :

y = 0
for string in x:
    y += float(string[:-1])
print(y)

Upvotes: 1

Related Questions