Tuples from List

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Feb 27 20:04:22 EST 2007


rshepard at nospam.appl-ecosys.com writes:

>   While it should be easy for me to get what I need from a list, it's
> proving to be more difficult than I expected.
>
>   I start with this list:
>
> [  6.24249034e-01+0.j   5.11335982e-01+0.j   3.67333773e-01+0.j
>    3.01189122e-01+0.j   2.43449050e-01+0.j   1.82948476e-01+0.j
>    1.43655139e-01+0.j   9.91225725e-02+0.j]

That's not correct syntax for a list. I assume, then, that it's not
actual code from your program.

> and I want a list of floats of only the first 6 digits for each value.

You don't get to choose how many digits are represented in a float
value; that's a property of the underlying floating-point
implementation, and indeed will change depending on the actual value
(since a float is a *binary* representation of a number, not decimal).

Perhaps you are looking for the Decimal type:

    <URL:http://docs.python.org/lib/module-decimal.html>

> 	for i in listname:
> 	    print i
>
> I get this:
> [each item printed separately]
>
>   I know it's embarrassingly simple, but the correct syntax eludes
> my inexperienced mind. What I want is a list [0.62424, 0.51133, ...]
> so that I can normalize those values.

You can create a new list from any sequence value by using the
constructor for the list type:

    >>> old_sequence = [12, 34, 56]
    >>> new_list = list(old_sequence)
    >>> new_list[0]
    12

As for making a list containing different values (e.g. Decimal
values), you might want a list comprehension:

    >>> from decimal import Decimal
    >>> old_sequence = [12, 34, 56]
    >>> new_list = [Decimal(value) for value in old_sequence]
    >>> new_list[0]
    Decimal("12")

-- 
 \         "I may disagree with what you say, but I will defend to the |
  `\    death your right to mis-attribute this quote to Voltaire."  -- |
_o__)                      Avram Grumer, rec.arts.sf.written, May 2000 |
Ben Finney




More information about the Python-list mailing list