Tuples from List

Discussion in 'Python' started by rshepard@nospam.appl-ecosys.com, Feb 28, 2007.

  1. -ecosys.com

    -ecosys.com Guest

    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]

    and I want a list of floats of only the first 6 digits for each value. If I
    write:
    for i in listname:
    print i

    I get this:

    (0.624249034424+0j)
    (0.511335982206+0j)
    (0.367333773283+0j)
    (0.301189121704+0j)
    (0.243449050439+0j)
    (0.182948475822+0j)
    (0.14365513894+0j)
    (0.0991225725344+0j)

    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.

    What is the correct syntax, please?

    Rich
    -ecosys.com, Feb 28, 2007
    #1
    1. Advertising

  2. -ecosys.com

    Paul Rubin Guest

    -ecosys.com writes:
    > [ 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]
    >
    > and I want a list of floats of only the first 6 digits for each value. If I
    > write:
    > for i in listname:
    > print i


    If you mean the first six digits of the real part and they're all < 1,

    for z in listname:
    print '%.5f' % z.real
    Paul Rubin, Feb 28, 2007
    #2
    1. Advertising

  3. -ecosys.com

    Robert Kern Guest

    -ecosys.com wrote:
    > 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]


    No, that's a numpy array.

    > and I want a list of floats of only the first 6 digits for each value. If I
    > write:
    > for i in listname:
    > print i
    >
    > I get this:
    >
    > (0.624249034424+0j)
    > (0.511335982206+0j)
    > (0.367333773283+0j)
    > (0.301189121704+0j)
    > (0.243449050439+0j)
    > (0.182948475822+0j)
    > (0.14365513894+0j)
    > (0.0991225725344+0j)


    Those aren't tuples, but complex numbers.

    > 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.
    >
    > What is the correct syntax, please?


    # Extract the real components (since the imaginary components are all 0):
    eigvals = eigvals.real

    # Normalize the eigenvalues:
    eigvals /= eigvals.sum()

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Feb 28, 2007
    #3
  4. -ecosys.com

    Ben Finney Guest

    -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
    Ben Finney, Feb 28, 2007
    #4
  5. -ecosys.com

    -ecosys.com Guest

    On 2007-02-28, Robert Kern <> wrote:

    > No, that's a numpy array.


    Robert,

    That's where I went off. I forgot that I'm still dealing with a 1D NumPy
    array and not a list. No wonder I had such fits!

    > Those aren't tuples, but complex numbers.


    I have not seen the 'j' suffix before. That was throwing me.

    > # Extract the real components (since the imaginary components are all 0):
    > eigvals = eigvals.real


    That's so much easier than what I ended up doing, which was creating
    another variable and assigning to that an explicit cast to real of the
    array.

    > # Normalize the eigenvalues:
    > eigvals /= eigvals.sum()


    Now that's really nice!

    Thank you very much for today's lessons. I really appreciate them

    Rich
    -ecosys.com, Feb 28, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. metiu uitem
    Replies:
    31
    Views:
    822
    Bengt Richter
    Nov 24, 2005
  2. Replies:
    5
    Views:
    552
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    572
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    721
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    228
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page