Python3:将字典值打印到'a':[1、2、3、5、6、9]到'a':1-3、5-6、9?

时间:2018-12-24 17:14:09

标签: python-3.x

我是Python的入门者,

我创建了一个字典,以文本的单词为键,值代表单词所在的行。

文本:

text = 'You needed me\nOoh, you needed me\nFeel a little more and give a little less\nKnow you hate to confess\nBut, baby, who, you needed me'

代码:

index = {}
line = text.split('\n')
for i, line in enumerate(line, 1):
    for word in line.split(' '):
           if word not in index:
                   index[word] = []
           index[word].append(i)

输出:

index 

{'You': [1], 'needed': [1, 2, 5], 'me': [1, 2, 5], 'Ooh,': [2], 'you': [2, 4, 5], 'Feel': [3], 'a': [3, 3], 'little': [3, 3], 'more': [3], 'and': [3], 'give': [3], 'less': [3], 'Know': [4], 'hate': [4], 'to': [4], 'confess': [4], 'But,': [5], 'baby,': [5], 'who,': [5]}

现在,我想订购连续数量的值,如下所示:

'a ': [1, 2, 3]'a ': 1-3

'a ': [1, 2, 3, 5, 6, 9]'a ': 1-3, 5-6, 9

2 个答案:

答案 0 :(得分:2)

intspan模块可能会适合您的需求:

>>> import intspan
>>> a = [1, 2, 3, 5, 6, 9]
>>> str(intspan.intspan(a))
'1-3,5-6,9'
>>> intspan.intspan(a).ranges()
[(1, 3), (5, 6), (9, 9)]

答案 1 :(得分:1)

使用标准库中的itertools.groupby

from itertools import groupby, count

def contiguous(a):
    c = count() 
    out = []
    for key, group in  groupby(a, key=lambda x: x-next(c)):
        group = list(group)
        g = str(group[0])
        if len(group) > 1:
            g += '-' + str(group[-1])
        out.append(g)
    return ', '.join(out)

print(contiguous([1, 2, 3, 5, 6, 9]))
# 1-3, 5-6, 9

这个想法是,在连续的组中,值和索引(我们使用count迭代器获得)之间的差异是恒定的,从而允许groupby将它们分组在一起。