我想知道是否有另一个命令把它缩短工作:蟒蛇...用数字
noes = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
这是我使用所以其具有年做的命令。
我想知道是否有另一个命令把它缩短工作:蟒蛇...用数字
noes = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
这是我使用所以其具有年做的命令。
noes = map(str, range(1, 16))
假设你真正想要的字符串。如果不是,那么noes = range(1, 16)
就足够了。
谢谢第一个工作:) – altin 2010-12-19 13:33:35
如果你是一个字符串列表后,您可以使用:
>>> x = [str(n) for n in range(1,16)] # or xrange if you wish
>>> x
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
号码列表可以用下面的进行:
x = [n for n in range(1,16)]
x = range(1,16)
这不是数字。你引用了它们,所以它们是字符串。数字会
noes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
而且短会
noes = range(1,16)
我建议你读一个Python教程。
使用
noes = range(1, 15);
或者,如果你真的需要字符串:
noes = [];
for i in range(1, 15):
noes.append(str(i))
我相信那你就用这个像
if AgeString in noes:
print "U R 2 yng!"
它可能是更清洁做逻辑比较,即
if int(AgeStr) < 16:
print "Too young"
这样的事情在[Python教程](http://docs.python.org/tutorial/)中有介绍。读这会节省你很多时间和麻烦。 – 2010-12-19 13:31:55