【问题标题】:How to find the Index range for consecutive duplicate elements in a list of list python?如何在列表 python 列表中找到连续重复元素的索引范围?
【发布时间】:2018-09-02 12:40:24
【问题描述】:

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

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

如何做到这一点??

我尝试循环,因为列表已经排序:

for i in range(0,len(A)-1):
    if A[i][1] == A[i+1][1]:
        print(i)

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

【问题讨论】:

  • 您只关心 inner 列表中的第一项吗?

标签: python list


【解决方案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}})

【讨论】:

    【解决方案2】:

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

    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.
    

    【讨论】:

      【解决方案3】:

      使用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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-05
        • 2019-12-31
        • 2016-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多