最简单的解释字符串的方法:数字,','和' - '?

时间:2017-05-18 01:10:01

标签: string list python-3.x

这有点模糊,但我正在编写一个执行以下操作的功能:

'1,2,3,4' -> [1,2,3,4]  
'1,2-4'   -> [1,2,3,4]  
'1-3,4'   -> [1,2,3,4]  
'1,1,1'   -> [1]  
'3-9, 1'  -> [1,3,4,5,6,7,8,9]  
'4-1,6'   -> [1,2,3,4,6]  

','表示前一个号码已完成,并将其添加到列表中 ' - '表示在两侧的两个数字之间包含所有数字 应避免使用空格,并应忽略重复

我有一些janky python代码让你知道我的目的:

def seasons_dict(seasons):

    season_dict = {}

    current = ''
    a = ''
    b = ''
    for i in range(len(seasons)):
        if seasons[i].isdigit():
            current = current + seasons[i]
            if (len(seasons) == (i + 1)) and (current not in season_dict):
                if a == '':
                    season_dict[current] = {}
                    current = ''
                else: 
                    b = current
                    if a == b:
                        season_dict[a] = {}
                        current = ''
                    elif b > a:
                        for s in range(int(a), int(b) + 1):
                            season_dict[str(s)] = {}
                        current = ''
                    elif a > b:
                        for s in range(int(b), int(a) + 1):
                            season_dict[str(s)] = {}
                        current = ''
        elif (seasons[i] == ','):
            if (a == '') and (current not in season_dict):
                season_dict[current] = {}
                current = ''
            elif (a != '') and (current not in season_dict):
                b = current
                if a == b:
                    season_dict[a] = {}
                    current = ''
                elif b > a:
                    for s in range(int(a), int(b) + 1):
                        season_dict[str(s)] = {}
                    current = ''
                elif a > b:
                    for s in range(int(b), int(a) + 1):
                        season_dict[str(s)] = {}
                    current = ''
                a = ''
            else:
                current = ''
        elif seasons[i] == '-':
            a = current
            current = ''

    return season_dict  

是的,我知道我原来说过这个列表,现在我正在处理字典,但它确实没有什么区别。列表通常更容易思考 谢谢大家!如果您有任何问题,请告诉我

2 个答案:

答案 0 :(得分:0)

我认为使用正则表达式来解析你的小迷你语言会让你大大简化代码:

CLLocationManager

这可能比你想要的更宽松一些,因为它忽略了任何不是数字或数字对的数字,中间有短划线。如果你给它import re def parse_numbers(num_str): results = set() for first, second in re.findall(r'(\d+)(?:\s*-\s*(\d+))?', num_str): if second: if int(first) > int(second): first, second = second, first results.update(range(int(first), int(second)+1)) else: results.add(int(first) return sorted(results) ,它会很乐意解析'1,2,foo,3-5'忽略[1, 2, 3, 4, 5]。如果您担心,可以编写一个单独的正则表达式来验证输入。同时验证和解析有点困难,所以我会分两次通过。

答案 1 :(得分:0)

您可以使用集合,排序,解析和范围:

tests=['1,2,3,4', 
'1,2-4',   
'1-3,4' , 
'1,1,1',   
'3-9, 1',    
'4-1,6',]

for s in tests:
    uniq=set()
    for e in s.split(','):
        if '-' in e:
            t=tuple(map(int, sorted(e.split('-',1))))
            uniq |= set(range(t[0],t[1]+1)) 
        else:
            uniq.add(int(e))
    li=sorted(uniq)

    print("{:10s} -> {}".format(s, li)) 

打印:

1,2,3,4    -> [1, 2, 3, 4]
1,2-4      -> [1, 2, 3, 4]
1-3,4      -> [1, 2, 3, 4]
1,1,1      -> [1]
3-9, 1     -> [1, 3, 4, 5, 6, 7, 8, 9]
4-1,6      -> [1, 2, 3, 4, 6]