如何在列表python列表中查找连续重复元素的索引范围?

时间:2018-09-02 12:40:24

标签: python list

我正在处理一个列表列表,我想要从元素开始复制的位置开始的列表索引。给定列表已按每个子列表的第二个键值按降序排序 IN

systemctl filter myService

我希望程序反映两个索引范围。

A = [[11, 89, 9], [12, 89, 48], [13, 64, 44], [22, 64, 56], [33, 64, 9]]

如何实现??

由于列表已排序,我试图循环:

1. 0 to 1 for 89 at 0th and 1st sublist
2. 2 to 4 for 64 at 2nd, 3rd and 4th sublist

但是它只返回起始索引,而不返回结束索引。

3 个答案:

答案 0 :(得分:1)

您可以使用collections.defaultdict创建具有set值的字典。迭代子列表和每个子列表中的项目,您可以为每个集合添加索引:

A = [[11, 89, 9], [12, 89, 48], [13, 64, 44], [22, 64, 56], [33, 64, 9]]

from collections import defaultdict

d = defaultdict(set)

for idx, sublist in enumerate(A):
    for value in sublist:
        d[value].add(idx)

print(d)

defaultdict(set,
            {11: {0}, 89: {0, 1}, 9: {0, 4}, 12: {1},
             48: {1}, 13: {2}, 64: {2, 3, 4},
             44: {2}, 22: {3}, 56: {3}, 33: {4}})

答案 1 :(得分:1)

这是您解决问题的另一种方法:

def rank(pos):
    return {1:"1st", 2:"2nd", 3:"3rd"}.get(pos, str(pos)+"th")

A = [[11, 89, 9], [12, 89, 48], [13, 64, 44], [22, 64, 56], [33, 64, 9]]

#Take every second element from each sublist.
B = [elem[1] for elem in A]

#Find all indices of those elements.
indices = [[elem, B.index(elem), B.index(elem) + B.count(elem)-1, [i for i in range(B.index(elem), B.index(elem) + B.count(elem))]] for elem in sorted(set(B), reverse=True)]

#Print formatted results.
for i in range(len(indices)):
    print("%d. " % (i+1), end="")
    print("%d to %d for %d at" % (indices[i][1],indices[i][2],indices[i][0]), end=" ")
    print(", ".join([rank(position) for position in indices[i][3][:-1]]), end=" ")
    print("and %s." % (rank(indices[i][3][-1])))

输出:

1. 0 to 1 for 89 at 0th and 1st.
2. 2 to 4 for 64 at 2nd, 3rd and 4th.

答案 2 :(得分:1)

使用itertools.groupby隔离组,然后保留多个项目。

import itertools
# generator to produce the first item in each *sub-list*.
b = (item[1] for item in a)

where = 0    # need this to keep track of original indices
for key, group in itertools.groupby(b):
    length = sum(1 for item in group)
    #length = len([*group])
    if length > 1:
        items = [where + i for i in range(length)]
        print(f'{key}:{items}')
        #print('{}:{}'.format(key, items))
    where += length

结果

89:[0, 1]
64:[2, 3, 4]

如果有人要将这个问题标记为重复,我将删除答案:重复/变量。...

What's the most Pythonic way to identify consecutive duplicates in a list?
Counting consecutive duplicates of strings from a list