简体   繁体   English

将用户的整数元组存储到列表中

[英]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 您无需显式调用split(''),因为split默认情况下会根据空间拆分字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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