Python: Using lower function on tuples

这一生的挚爱 提交于 2019-12-06 08:08:18

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.

Michał Szydłowski

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

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]]

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