简体   繁体   中英

getting space separated input in python

I am trying to get space separated inputs. while the first method works completely fine, the second method throws an error saying:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

what is wrong with the second method?

Method 1:

x = [int(j) for j in input().split()]

Method 2:

x = [j for j in int(input().split())]

Because you are using split() to a string which will return a list , then you are passing this list to int() that's why you are getting error. for changing datatype of list you need to use map() as below or first approach of your's.

Try Below code

x = [j for j in map(int,input().split())]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM