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

Tuesday, July 19, 2022

[FIXED] How can I split and map input like 'S1,10' to two variables one str and other integer

 July 19, 2022     input, integer, python, split, string     No comments   

Issue

I am reading an input 'S1,10'. First component is a string and second is an integer. They are separated by comma.

I have tried x = input().split(','). This creates a list ['S1','10']. How can I create a list ['S1', 10] where the second element is an integer?

I have solved this in a two step process. bp = input().split(',') bp[1] = int(bp[1])

Can it be done in a single step? How can we split with different datatypes?


Solution

I mean, if you really want to force it, it can be done in a single line

x = [s if i == 0 else int(s) for i,s in enumerate(input().split(','))]

But at the end of the day, code is for humans to understand. If I were you I would keep what you have.



Answered By - Allie
Answer Checked By - Pedro (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