Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96528

Re: convert element in a list to float

From Laura Creighton <lac@openend.se>
Subject Re: convert element in a list to float
References <417abfa4-e7c7-4573-83ed-50b8a5aca2b2@googlegroups.com>
Date 2015-09-13 23:02 +0200
Newsgroups comp.lang.python
Message-ID <mailman.500.1442178190.8327.python-list@python.org> (permalink)

Show all headers | View raw


In a message of Sun, 13 Sep 2015 12:55:13 -0700, forums_mp@hotmail.com writes:
>
>
>For starters, I googled and saw a plethora of writings on how to convert an entire list from string to float.   My interest is on select elements in the list.  The output from the print statement: print scenarioList 
>  
>is as follows
>
>[ '3000000', '"N"', '11400000', '"E"' ]
>
>I need to convert the first and third element to float.  
>  lat = ( float ) scenarioList [ 0 ]
>  lon = ( float ) scenarioList [ 2 ]
>
>fails (invalid syntax).  How can I achieve my objective. 
>
>Thanks in advance

Does this help?

>>> l = [ '3000000', '"N"', '11400000', '"E"' ]
>>> [float(l[0]), l[1], float(l[2]), l[3]]
[3000000.0, '"N"', 11400000.0, '"E"']

Laura

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

convert element in a list to float forums_mp@hotmail.com - 2015-09-13 12:55 -0700
  Re: convert element in a list to float Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-13 21:07 +0100
  Re: convert element in a list to float Laura Creighton <lac@openend.se> - 2015-09-13 23:02 +0200
    Re: convert element in a list to float Denis McMahon <denismfmcmahon@gmail.com> - 2015-09-13 23:43 +0000
  Re: convert element in a list to float Steven D'Aprano <steve@pearwood.info> - 2015-09-14 11:09 +1000

csiph-web