x = "001,008-011,020"
x = x.split(',') # Split at the commas
y = []
# Iterate over the list
for i in x:
try:
# Will append the integer to your output list
y.append(int(i))
except ValueError:
# If you get a ValueError (from trying to int('008-011') for example)
# then split the string at the hyphen and individually append
# the integers in between.
j = i.split('-')
for k in range(int(j[0]), int(j[1])+1):
y.append(k)
你可以做如下的事情。。在
见^{}
关于:
之后呢
^{pr2}$如果你知道字符串的格式是。。。在
我认为这应该行得通,不过您可能需要检查try/except循环中是否会无意中捕获到其他ValueErrors。在
相关问题 更多 >
编程相关推荐