LoginSignup
1
0

More than 5 years have passed since last update.

ハイフンで範囲を示した数の展開

Last updated at Posted at 2018-05-20

'3,5,7-10' → [3,5,7,8,9,10]

def expand_numbers(s,nmin=1,nmax=100):
    a=[]
    for v in re.split('[\s,]+',s):
        if '-' in v: 
            v0,v1=v.split('-')[:2]
            if v0 == '': v0=nmin
            if v1 == '': v1=nmax
            a.extend(range(int(v0),int(v1)+1))
        else:
            a.append(int(v))
    return list(set(a))
expand_numbers('3,5,7-10')
[3, 5, 7, 8, 9, 10]

expand_numbers('-5,8')
[1, 2, 3, 4, 5, 8]

expand_numbers('3-5,4-7')
[3, 4, 5, 6, 7]

コメント

参考文献

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0