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

Wednesday, July 20, 2022

[FIXED] How to convert numeric string from a sublist in Python

 July 20, 2022     integer, list, numeric, python, sublist     No comments   

Issue

I'm a freshie. I would like to convert a numeric string into int from a sublist in Python. But not getting accurate results. 😔

countitem = 0
list_samp = [['1','2','blue'],['1','66','green'],['1','88','purple']]

for list in list_samp:
  countitem =+1
  for element in list:
    convert_element = int(list_samp[countitem][0])
    list_samp[countitem][1] = convert_element

Solution

You can do it like this:

list_samp = [['1','2','blue'],['1','66','green'],['1','88','purple']]
me = [[int(u) if u.isdecimal() else u for u in v] for v in list_samp]
print(me)


Answered By - Ajeigbe Oladayo
Answer Checked By - Mary Flores (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