0

I want to assign values to variable using .split()

I want to pass values in a single line with spaces ex:- 2 3 1 1 1. 2 should be assigned to A , 3 should be assigned to B and the remaining numbers should be assigned to a list .How can I do it? I tried A,B,C = map(int, input ().split()) but it shows error

23rd Jan 2022, 12:51 PM
syed fahad
syed fahad - avatar
8 Respostas
+ 4
e.g.: a, b, *c = input().split() Note the *
23rd Jan 2022, 12:57 PM
Lisa
Lisa - avatar
+ 4
You were almost there: A, B, *C = map(int, input().split())
23rd Jan 2022, 12:57 PM
Angelo
Angelo - avatar
+ 2
Try A, B, *C = map(..blah blah...), If not got my ide up and running at the moment and it's been a while since I've done any python.
23rd Jan 2022, 12:57 PM
rodwynnejones
rodwynnejones - avatar
+ 2
If only the last values are int, you could try to only convert the non-string variables: a, b, *c = input().split() a, b = int(a), int(b)
25th Jan 2022, 7:19 AM
Lisa
Lisa - avatar
+ 1
you have to prefix C with * like *C so that it can assign the rest of the numbers, as it is variable. So: A,B,*C = map(int, input().split()) without it it would throw an error because the rest has not been assigned
23rd Jan 2022, 1:01 PM
Kamil Hamid
Kamil Hamid - avatar
0
Thanks Everyone
23rd Jan 2022, 1:02 PM
syed fahad
syed fahad - avatar
0
one more question what if the last values are string type?
23rd Jan 2022, 1:14 PM
syed fahad
syed fahad - avatar
0
n = int(Input ()) ls = list(n.split()) A = ls[0] B = ls[1] simplest one, don't go for mapped ,zip functions it will only drain your brain's battery
25th Jan 2022, 7:09 AM
Saad Khan
Saad Khan - avatar