Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #96522 > unrolled thread
| Started by | forums_mp@hotmail.com |
|---|---|
| First post | 2015-09-13 12:55 -0700 |
| Last post | 2015-09-14 11:09 +1000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | forums_mp@hotmail.com |
|---|---|
| Date | 2015-09-13 12:55 -0700 |
| Subject | convert element in a list to float |
| Message-ID | <417abfa4-e7c7-4573-83ed-50b8a5aca2b2@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-09-13 21:07 +0100 |
| Message-ID | <mailman.495.1442174874.8327.python-list@python.org> |
| In reply to | #96522 |
On 13/09/2015 20:55, forums_mp@hotmail.com wrote: > > 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 > Strong hint, you do not cast the strings to floats, you call the builtin float() function to do the conversion. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Laura Creighton <lac@openend.se> |
|---|---|
| Date | 2015-09-13 23:02 +0200 |
| Message-ID | <mailman.500.1442178190.8327.python-list@python.org> |
| In reply to | #96522 |
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
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-09-13 23:43 +0000 |
| Message-ID | <mt51mf$ji9$2@dont-email.me> |
| In reply to | #96528 |
On Sun, 13 Sep 2015 23:02:55 +0200, Laura Creighton wrote: > 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"'] Here's a method that will convert any value in a list that can be made a float into a float, and (I think) should leave all others as they are. It users a helper function and a list comprehension. >>> def tofloat(x): ... try: ... return float(x) ... except ValueError: ... return None ... >>> l = [ '3000000', '"N"', '11400000', '"E"' ] >>> l = [ tofloat(x) or x for x in l ] >>> l [3000000.0, '"N"', 11400000.0, '"E"'] -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-09-14 11:09 +1000 |
| Message-ID | <55f61e38$0$1668$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #96522 |
Hi Forums_MP and welcome, On Mon, 14 Sep 2015 05:55 am, forums_mp@hotmail.com wrote: > 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. By now you have hopefully discovered that the answer is to call float as a function: lat = float(scenarioList[0]) but I wonder what in the documentation or examples you saw suggested to you that Python used the C type-cast syntax `(float)value`? Whatever it was that gave you this wrong impression needs to be fixed. -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web