2 回答
TA贡献1821条经验 获得超6个赞
正则表达式对我有用,将它们直接应用于所返回的值raw_input()。例如:
import re
s1 = '1321 .. 123123'
s2 = '-21323 , 1312321'
s3 = '- 12312.. - 9'
[int(x) for x in re.findall(r'[^,.]+', ''.join(s1.split()))]
=> [1321, 123123]
[int(x) for x in re.findall(r'[^,.]+', ''.join(s2.split()))]
=> [-21323, 1312321]
[int(x) for x in re.findall(r'[^,.]+', ''.join(s3.split()))]
=> [-12312, -9]
TA贡献1810条经验 获得超4个赞
def ask_range():
raw = raw_input()
lis = []
split1 = raw.split("..")
for i in split1:
try:
lis.append(int(i))
except:
for j in i.split(","):
list.append(int(j))
if len(raw) != 2:
print "\nexpecting a range value, try again."
return ask_range()
return lis
首先使用..然后拆分,
我认为这会有所帮助。
添加回答
举报