简体   繁体   中英

Split a string if there is space

Suppose i have set of inputs that contain integers separated by space or it may contain a single integer, Is there a way to take input in single line using raw_input().split() if space found otherwise just raw_input() . (In python 2.x)

For example :

Input : 1 2 3 4 5

In this case we can use :

     Integers=map(int,raw_input().split(' '))

Input: 2

In this case :

    Integer=int(raw_input()) 

Is there a way to combine these two in some pythonic way in one line?

Use the split version, it would return a single element list when only a single integer is given to raw_input .

>>> Integers=map(int,raw_input().split())
1
>>> Integers
[1]

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