Python: Using lower function on tuples

10,127

Solution 1

Parsing of string to a floating point number works with both uppercase 'E' and lowercase 'e'.

Your code can be shortened to:

EarthU = ['1.3719107E+11', '8.3311764E-02', '2.2719107E+11', '1.4880643E+03']
earthU = [(0.3048*0.3048)*float(element) for element in earthU]

And for tuples you can use a single list comprehension by extracting the elements of tuples (since tuple itself doesn't have .lower() method but its elements do):

EarthV = [('4.2997980E+12', '7.5608735E+13'), ('1.8986931E+00', '3.0367303E+02'), ('3.4997980E+12', '7.5608735E+13'), ('-4.9202352E+04', '2.8277192E+06')]
earthV = [(float(x), float(y)) for x,y in EarthV]

If you really need lowercase:

earthV = [(x.lower(), y.lower()) for x,y in EarthV]

This form for x,y in EarthV destructures the element of EarthV by taking the first part of the tuple element and binds it to x and the second part of the tuple binds to y.

Solution 2

Because it doesn't. You could iterate over the tuple fields first, and then over the entire list.

Since tuple doesn't have a .lower method, use a nested list comprehension to get a list of list corresponding to your original list of tuples:

>>> EarthV = [('4.2997980E+12', '7.5608735E+13'), ('1.8986931E+00', '3.0367303E+02'), ('3.4997980E+12', '7.5608735E+13'), ('-4.9202352E+04', '2.8277192E+06')]
>>> [[x.lower()  for x in element] for element in EarthV]
[['4.2997980e+12', '7.5608735e+13'], ['1.8986931e+00', '3.0367303e+02'], ['3.4997980e+12', '7.5608735e+13'], ['-4.9202352e+04', '2.8277192e+06']]
Share:
10,127
Admin
Author by

Admin

Updated on June 13, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm new to Python and have looked at quite a bit of documentation to figure out what is going on, but haven't had any luck.

    I have a list of tuples that I need to convert to lowercase and perform mathematical operations on all values in the list. The "E", needs to become an "e" in order to perform mathematical operations.

    If there is a single value in a given list of tuples, the following works:

    EarthU = ['1.3719107E+11', '8.3311764E-02', '2.2719107E+11', '1.4880643E+03']
    earthU = [element.lower() for element in EarthU]
    earthU = [(0.3048*0.3048)*float(element) for element in earthU]
    

    If there are more than one value for each tuple in a given list of tuples and I try the same logic:

    EarthV = [('4.2997980E+12', '7.5608735E+13'), (1.8986931E+00', '3.0367303E+02'), ('3.4997980E+12', '7.5608735E+13'), ('-4.9202352E+04', '2.8277192E+06')]
    earthV = [element.lower() for element in EarthV]
    

    And I receive the following error when trying to convert each element in the tuple to lowercase:

    AttributeError: 'tuple' object has no attribute 'lower'

    I have a feeling that this attribute error I am running into will become a problem when I try to perform the mathematical operations as well. Thanks.

  • Admin
    Admin about 9 years
    Using Michal's answer yielded the correct response. Thanks!
  • Michał Szydłowski
    Michał Szydłowski about 9 years
    glad I could help, think of accepting one of the answers as correct