简体   繁体   中英

Storing a tuple of integers from user into a list

I am supposed to take input of two numbers separated by spaces from the user, save it in a tuple and then put that tuple in a list.

What I tried was

>>> n=[]
>>> n.append(tuple(raw_input().strip().split(' ')))
1 2

the output was

>>> n
[('1','2')]

I require an output of the form

>>> n
[(1,2)]

Cast the output into integers:

>>> n=[]
>>> n.append(tuple(int(x) for x in raw_input().strip().split(' ')))
1 2
>>> n
[(1, 2)]

Use map

>>> n = []
>>> n.append(tuple(map(int, raw_input().strip().split())))

You need not call split(' ') explicitly as split splits the string based on space by default

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