NegaOverflow
NegaOverflow

Reputation: 138

Extract numbers from decimal list

I searched around not found suitable answers, maybe a simple question but really stop me forward:

aList = [Decimal('-1.23'), Decimal('4.56')]

how to extract only the numbers: ['-1.23', '4.56']?

any better methods than using split or re.findall()

Many thanks!

Upvotes: 1

Views: 129

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477180

You can use str(…) [python-doc] to get a textual presentation of the decimal, for example:

>>> [str(a) for a in aList]
['-1.23', '4.56']

Now these are strings.

You can furthermore use float(…) [python-doc] to convert it to the nearest representable floating point number:

>>> [float(a) for a in aList]
[-1.23, 4.56]

Upvotes: 1

Related Questions