[英]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使用
enumerate
和defaultdict
,您可以使用以下命令构建值到其索引的映射
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]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.