Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Input as list?

I am getting input in the form of:

4 11111

I am using a,b = map(int,raw_input().split()) to store 4 in a and 11111 in b. But I want b to be a list, how am I supposed to do that ?

like image 901
John Constantine Avatar asked Jun 02 '26 18:06

John Constantine


2 Answers

You can just use list :

>>> list('11111')
['1', '1', '1', '1', '1']

But in that case you can not use map function because it just apply one function on its iterable argument and in your code it convert the whole of '11111' to integer so you have tow way :

  1. create b as a list of string ones :
inp=raw_input().split()
a,b = int(inp[0]),list(inp[1])
  1. if you want a list of integer 1's use map :
>>> map(int,'11111')
[1, 1, 1, 1, 1]
like image 91
Mazdak Avatar answered Jun 05 '26 06:06

Mazdak


You can try this in two steps:

a, b = raw_input().split()
a, b = int(a), map(int, b)
print a
print b

Returns: 4 and [1, 1, 1, 1, 1]

like image 38
Christophe Avatar answered Jun 05 '26 06:06

Christophe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!