简体   繁体   English

从列表创建等值索引的子列表

[英]Create sublists of indexes of equal values from list

I'm trying to split a list of integers into sublists of the the indexes of equal integers.我试图将整数列表拆分为相等整数索引的子列表。 So say I have a list:所以说我有一个清单:

original_list = [1,2,1,4,4,4,3,4,4,1,4,3,3]

The desired output would be:所需的输出是:

indexes : [[0,2,9], [1], [6,11,12], [3,4,5,7,8,10]]
# corresponds to sublists: [[1,1,1] [2], [3,3,3], [4,4,4,4,4,4]]

I can't figure out how to do this though, as most solutions require you to first sort the original list, but in my case, this messes up the indices.我不知道如何做到这一点,因为大多数解决方案要求您首先对原始列表进行排序,但在我的情况下,这会弄乱索引。 Itertools or np.arrays have not helped me for this reason, as they only group sequential equal elements.由于这个原因,Itertools 或 np.arrays 对我没有帮助,因为它们只对顺序相等的元素进行分组。

Does anyone know of a solution for this problem?有谁知道这个问题的解决方案? I would love to hear!我很想听听!

You can use enumerate :您可以使用enumerate

original_list = [1,2,1,4,4,4,3,4,4,1,4,3,3]
groups = {a:[i for i, c in enumerate(original_list) if c == a] for a in set(original_list)}

Output:输出:

{1: [0, 2, 9], 2: [1], 3: [6, 11, 12], 4: [3, 4, 5, 7, 8, 10]}

Using enumerate and a defaultdict , you can build a mapping of values to their indices with使用enumeratedefaultdict ,您可以使用以下命令构建值到其索引的映射

from collections import defaultdict

dd = defaultdict(list)
for index, value in enumerate(original_list):
    dd[value].append(index)

print(dd)
# defaultdict(<class 'list'>, {1: [0, 2, 9], 2: [1], 4: [3, 4, 5, 7, 8, 10], 3: [6, 11, 12]})

You can use collections.defaultdict for a one-pass solution.您可以使用collections.defaultdict进行一次性解决方案。 Then use sorted if you need, as in your desired result, to sort your indices by value.然后根据需要使用sorted ,如您想要的结果,按值对索引进行排序。

original_list = [1,2,1,4,4,4,3,4,4,1,4,3,3]

from collections import defaultdict
from operator import itemgetter

dd = defaultdict(list)

for idx, value in enumerate(original_list):
    dd[value].append(idx)

keys, values = zip(*sorted(dd.items(), key=itemgetter(0)))

print(keys, values, sep='\n')

(1, 2, 3, 4)
([0, 2, 9], [1], [6, 11, 12], [3, 4, 5, 7, 8, 10])

For comparison, the values of dd are insertion ordered in Python 3.6+ (officially in 3.7+, as a CPython implementation detail in 3.6):为了比较, dd的值在 Python 3.6+ 中是按插入顺序排列的(官方在 3.7+ 中,作为 3.6 中的 CPython 实现细节):

print(list(dd.values()))

[[0, 2, 9], [1], [3, 4, 5, 7, 8, 10], [6, 11, 12]]

Here is how I would do it with numpy, using the argsort function I linked in the comments.这是我将如何使用 numpy 进行操作,使用我在评论中链接的 argsort 函数。

original = [1,2,1,4,4,4,3,4,4,1,4,3,3]
indexes = []
s = set()

for n in np.argsort(original):
    if original[n] in s:
        indexes[-1].append(n)
    else:
        indexes.append([n])
        s.add(original[n])

print(indexes)

This can be achieved with a list comprehension.这可以通过列表理解来实现。

>>> x = [1,2,1,4,4,4,3,4,4,1,4,3,3]
>>> [[i for i in range(len(x)) if x[i]==y] for y in sorted(set(x))]
[[0, 2, 9], [1], [6, 11, 12], [3, 4, 5, 7, 8, 10]]

Here's a linear time and space solution that appends indexes to number bucket lists in an OrderedDict() and extracts the index lists to produce the result: 这是一个线性时间和空间解决方案,它将索引附加到OrderedDict()中的数字桶列表,并提取索引列表以生成结果:

from collections import OrderedDict

original_list = [9,2,9,4,4,4,3,4,4,9,4,3,3]
counts = OrderedDict()

for i, e in enumerate(original_list):
    if e not in counts:
        counts[e] = []
    counts[e].append(i)

print(list(counts.values()))

Output: 输出:

[[0, 2, 9], [1], [6, 11, 12], [3, 4, 5, 7, 8, 10]]

Try it! 试试吧!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PYTHON:将相同值的子列表从嵌套列表创建到一个列表中 - PYTHON: Create sublists of same values from nested list into one list 创建一个包含 100 个整数的列表,其值等于它们的索引 - Create a list of 100 integers whose values equal their indexes Python:从给定列表创建等长子列表的有效方法,允许重复且顺序无关紧要 - Python: Efficient way to create equal length sublists from given list with repetition allowed and order does not matter 将值从 1 个列表传递到矩阵中的子列表 - Passing Values from 1 List to Sublists in a Matrix 从索引列表创建子列表的 Pythonic 方法 - Pythonic way to create sublists from a list of indices 将值列表附加到子列表 - append list of values to sublists python - 有没有办法使用列表理解根据提取的子列表的公共索引创建列表? - python - is there a way to use list comprehension to create a list based on the extracted common indexes of sublists? 根据 Python 中的一组索引将列表拆分为子列表 - Split a list into sublists based on a set of indexes in Python 需要创建一个包含 100 个子列表的列表,每个子列表包含 3 个随机值 - Need to create a list of 100 sublists that contain 3 random values each 如何从另一个子列表列表的特定元素创建新的子列表列表 - How to create new list of sublists from specific elements of another list of sublists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM