Python: Using lower function on tuples

末鹿安然 提交于 2020-01-02 09:11:35

问题


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.


回答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.




回答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']]



回答3:


Can also iterate over lists as a substitute for tuples

EarthU = [['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 = []
for x in EarthU:
   z = []
   for y in x:
       y = str(y).replace('E','e')
       y =  (0.3048*0.3048)*float(y)
       z.append(y)
   earthv.append(z)
print earthv

output:

[[399464305585.92004, 7024281332054.4], [0.17639436101702402, 28.2121476530112], [325141873585.92004, 7024281332054.4], [-4571.048075950081, 262703.709946368]]



回答4:


The reason that you receive the AttributeError: 'tuple' object has no attribute 'lower' is because you are applying the .lower() to a tuple as opposed to a string. You need to first iterate over the elements in the tuple and apply .lower() then apply your mathematical operation. This can be performed in one line using list comprehensions and generator expression as follows (w/o using .lower()):

In [7]:

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 = [tuple((0.3048*0.3048)*float(element) for element in tple) for tple in EarthV]
print earthV


[(399464305585.92004, 7024281332054.4), (0.17639436101702402, 28.2121476530112), (325141873585.92004, 7024281332054.4), (-4571.048075950081, 262703.709946368)]


来源:https://stackoverflow.com/questions/28463826/python-using-lower-function-on-tuples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!