PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 10, 2022

[FIXED] How to convert string to decimal in this list of lists?

 August 10, 2022     decimal, list, python, string-conversion     No comments   

Issue

I'm trying to convert the strings in this list of lists into decimal form:

ask = [['31188.28758860', 2], ['31183.48445986', 0.14258435], ...]

I tried using elem.strip('"') for elem in ask to remove the quote marks but get "AttributeError: 'list' object has no attribute 'strip'" So I assume I am not accessing the nested level of the strings


Solution

[float(elem[0]) for elem in ask]

to just get the first values of your nested list.

OR

[[float(elem[0]), elem[1]] for elem in ask]

to get the same structure than before, but with the strings replaced by floats.



Answered By - Nicolas Julémont
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing