2010-03-21 34 views
0

让我说我想从标准输入读取整数a,b和c(在一行中,不需要在每个数字后按回车)。在C++中,我只会这样做:在Python中阅读更多数字

cin >> a >> b >> c;

如何在Python中做到这一点?

回答

3
values = raw_input() 
# 1 3 15 
a, b, c = values.split() 

a'1'b'3'c'15'


如果你想成为额外的短,并得到整数试试这个:

a, b, c = [int(_) for _ in raw_input().split()] 
+1

他们将字符串,而不是整数。 – FogleBird 2010-03-21 21:32:04

+0

请注意''raw_input'是推荐用于Python 2的版本。如此之多,它现在在Python 3中拼写为'input'(并且旧的Python 2' input'已经消失)。 http://www.python.org/dev/peps/pep-3100/ – bignose 2010-03-21 22:13:24

3

字符串

a,b,c=raw_input().split() 

对于int

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