1
iLig1, iCol1 , iLig2 , iCol2 , carac = map(int, input ().split ())

hello I try to get this ligne of input in my code right, I get 5 variables from an input that look like this:

1 12 7 14 u

how can I declare the last one as a str properly, I tried to consider them all as str and convert the 4 first as int but I cannot interpret str as int (you know..)

Thank you for your help !

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • You don't seem to be *declaring* anything. Process the first 4 items with one line of code, and the last with a different line of code. – Scott Hunter Nov 01 '22 at 18:13
  • 2
    Please update your question with the code that `"convert the 4 first as int"`. – quamrana Nov 01 '22 at 18:13

3 Answers3

3

You can use the Iterable Unpacking : (*).

# we suppose input is -> 1 2 3 4 u
>>> *i, j = input().split()

>>> print(i)
['1', '2', '3', '4']

>>> print(j)
u

>>> list(map(int, i))
[1, 2, 3, 4]
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • 2
    May as well unpack on the same line as the `split`; no real benefit to not doing so, and it demonstrates a benefit of unpacking that simple slicing and indexing can't match cleanly. – ShadowRanger Nov 01 '22 at 18:28
  • 1
    Thank you for the unpacking operator (*) tips, I learned something new and this is basically exactly what I was looking for! – Clément Maujean Nov 01 '22 at 18:31
  • @ClémentMaujean, You're welcome, reading this [link](https://stackoverflow.com/questions/50950690/python-unpacking-operator) is good. Maybe for more detail and use unpacking in the middle. – I'mahdi Nov 01 '22 at 18:38
2

This should do the job:

iLig1, iCol1 , iLig2 , iCol2 , carac = [int(x) if i<4 else x for i,x in enumerate(input ().split ())]
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

One liner that converts the first 4 to int and the last to str

iLig1, iCol1 , iLig2 , iCol2 , carac = map(lambda f: f[1](f[0]), zip(input().split(), [int, int, int, int, str]))
jvx8ss
  • 529
  • 2
  • 12