I have a list containing vectors (float x, y ,z values) and i'm trying group the same values together....
我有一个包含向量的列表(浮点数x,y,z值),我正在尝试将相同的值组合在一起....
Here is a working example on a single list:
以下是单个列表上的工作示例:
from collections import defaultdict
example_list = [1,5,1,4,2,5,5,4,5,7,2,1,7,9]
d = defaultdict(list)
for item in example_list:
d[item].append(item)
groupedlist = sorted(d[x] for x in d)
# Returns [[1, 1, 1], [2, 2], [4, 4], [5, 5, 5, 5], [7, 7], [9]]
I'm trying the achieve the same result for nested lists of 3D Vectors (X,Y,Z)...
我正在尝试为3D矢量(X,Y,Z)的嵌套列表实现相同的结果......
example_vectorlist = [[1,2,4], [1,2,3], [3,4,3], [1,3,2], [5,6,7], [3,4,3], [5,6,7]]
# Desired output = [[1,2,4],[[1,2,3],[1,2,3]],[1,3,2],[[3,4,3], [3,4,3]],[[5,6,7], [5,6,7]]
3 个解决方案
#1
1
Just make the keys for your defaultdict
into tuples:
只需将defaultdict的键设置为元组:
from collections import defaultdict
example_list = [[1,2,4], [1,2,3], [3,4,3], [1,3,2], [5,7,1], [3,4,3], [5,6,1]]
d = defaultdict(list)
for item in example_list:
d[tuple(item)].append(item)
groupedlist = sorted(d[x] for x in d)
The problem with just using the original "vectors" as keys to d
is that lists aren't hashable; making tuples of them addresses this.
仅使用原始“向量”作为d的键的问题是列表不可清除;制作他们的元组可以解决这个问题。
#2
1
Without using defaultdict
:
不使用defaultdict:
example_vectorlist = [[1,2,4], [1,2,3], [3,4,3], [1,3,2], [5,7,1],[3,4,3], [5,6,1]]
d = {}
for el in example_vectorlist:
try:
d[tuple(el)].append(el)
except KeyError:
d[tuple(el)] = [el]
print d.values()
#3
0
Your desired output does not reflect what you have as input, if you want to group common sublists you can use itertools.groupby
, considering you want sorted output using a dict and then sorting makes less sense than just creating the groupings from the sorted list using groupby:
您想要的输出并不能反映出您输入的内容,如果您想对可以使用itertools.groupby的公共子列表进行分组,考虑到您希望使用dict排序输出,然后排序不仅仅是使用排序列表创建分组通过...分组:
from itertools import groupby
print([list(v) for _,v in groupby(sorted(example_vectorlist))])
Which for you original list outputs the same:
哪个为您原始列表输出相同:
example_list = [1,5,1,4,2,5,5,4,5,7,2,1,7,9]
print([list(v) for _,v in groupby(sorted(example_list))])
[[1, 1, 1], [2, 2], [4, 4], [5, 5, 5, 5], [7, 7], [9]]
#1
1
Just make the keys for your defaultdict
into tuples:
只需将defaultdict的键设置为元组:
from collections import defaultdict
example_list = [[1,2,4], [1,2,3], [3,4,3], [1,3,2], [5,7,1], [3,4,3], [5,6,1]]
d = defaultdict(list)
for item in example_list:
d[tuple(item)].append(item)
groupedlist = sorted(d[x] for x in d)
The problem with just using the original "vectors" as keys to d
is that lists aren't hashable; making tuples of them addresses this.
仅使用原始“向量”作为d的键的问题是列表不可清除;制作他们的元组可以解决这个问题。
#2
1
Without using defaultdict
:
不使用defaultdict:
example_vectorlist = [[1,2,4], [1,2,3], [3,4,3], [1,3,2], [5,7,1],[3,4,3], [5,6,1]]
d = {}
for el in example_vectorlist:
try:
d[tuple(el)].append(el)
except KeyError:
d[tuple(el)] = [el]
print d.values()
#3
0
Your desired output does not reflect what you have as input, if you want to group common sublists you can use itertools.groupby
, considering you want sorted output using a dict and then sorting makes less sense than just creating the groupings from the sorted list using groupby:
您想要的输出并不能反映出您输入的内容,如果您想对可以使用itertools.groupby的公共子列表进行分组,考虑到您希望使用dict排序输出,然后排序不仅仅是使用排序列表创建分组通过...分组:
from itertools import groupby
print([list(v) for _,v in groupby(sorted(example_vectorlist))])
Which for you original list outputs the same:
哪个为您原始列表输出相同:
example_list = [1,5,1,4,2,5,5,4,5,7,2,1,7,9]
print([list(v) for _,v in groupby(sorted(example_list))])
[[1, 1, 1], [2, 2], [4, 4], [5, 5, 5, 5], [7, 7], [9]]