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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.