英文:
Three column Numpy array to a dict of tuples?
问题
将一个三列数组转换成一个以一列作为键,值为元组列表的字典的最快/最有效的方法如下:
import numpy as np
arr = np.array([[1,2,3,3],[5,6,7,8],[8,7,6,5]]).T
# 转换成字典
result_dict = {}
for row in arr:
key = row[0]
values = [(row[1], row[2])]
if key in result_dict:
result_dict[key].extend(values)
else:
result_dict[key] = values
result_dict
得到的结果将是:
{1: [(5, 8)], 2: [(6, 7)], 3: [(7, 6), (8, 5)]}
英文:
So I am trying to find the fastest/most efficient way of performing the following transformation.
From a three column array
import numpy as np
arr = np.array([[1,2,3,3],[5,6,7,8],[8,7,6,5]]).T
array([[1, 5, 8],
[2, 6, 7],
[3, 7, 6],
[3, 8, 5]])
into a dict keyed off of one column with a list of tuples as the value, as such:
{1:[(5,8)], 2:[(6,7)],3: [(7,6),(8,5)]}
答案1
得分: 2
根据注释中的说明,如果您正在构建一个字典,一个Python对象,那么您只需使用常规的Python方法即可。我建议在数组上使用.tolist()
,因为这会转换为适当的Python数值类型(但它确实会生成一个中间列表)。因此,从那里,您只需使用您喜欢的Python字典分组习惯:
result = {}
for key, *vals in arr.tolist():
result.setdefault(key, []).append(tuple(vals))
如果您不想创建一个可能巨大的中间列表,那么您可以手动转换类型(在这里,我假设基于您的示例是int
类型):
result = {}
for row in arr:
key, *vals = map(int, row)
result.setdefault(key, []).append(tuple(vals))
英文:
As noted in the comments, if you are constructing a dict, a python object, then you just want to go with regular Python methods. I suggest using .tolist()
on your array, because this converts to appropriate Python numeric types (but it does make an intermediate list). So from there, you just use your favorite Python dict grouping idiom:
result = {}
for key, *vals in arr.tolist():
result.setdefault(key, []).append(tuple(vals))
If you don't want to create a potentially giant intermediate list, then you manually convert the type (here I'm assuming int
based on your example):
result = {}
for row in arr:
key, *vals = map(int, row)
result.setdefault(key, []).append(tuple(vals))
答案2
得分: 1
In [257]: from collections import defaultdict
In [258]: arr = np.array([[1,2,3,3],[5,6,7,8],[8,7,6,5]]).T
In [259]: arr
Out[259]:
array([[1, 5, 8],
[2, 6, 7],
[3, 7, 6],
[3, 8, 5]])
使用defaultdict可以轻松收集相关的值。只需定义一个使列表值成为默认值的defaultdict。
In [260]: dd = defaultdict(list)
然后,只需添加元素,以第一个元素作为键,其余元素作为元组(附加到默认值[])。
In [261]: for i in arr.tolist():
...: dd[i[0]].append(tuple(i[1:]))
...:
In [262]: dd
Out[262]: defaultdict(list, {1: [(5, 8)], 2: [(6, 7)], 3: [(7, 6), (8, 5)]})
在迭代方面,列表比数组稍快。
英文:
In [257]: from collections import defaultdict
In [258]: arr = np.array([[1,2,3,3],[5,6,7,8],[8,7,6,5]]).T
In [259]: arr
Out[259]:
array([[1, 5, 8],
[2, 6, 7],
[3, 7, 6],
[3, 8, 5]])
A defaultdict makes collecting 'related' values easy. Just define one that makes a list value as default value.
In [260]: dd = defaultdict(list)
Then just add the elements, using the first element as key, the rest as a tuple (appended to the default []).
In [261]: for i in arr.tolist():
...: dd[i[0]].append(tuple(i[1:]))
...:
In [262]: dd
Out[262]: defaultdict(list, {1: [(5, 8)], 2: [(6, 7)], 3: [(7, 6), (8, 5)]})
For iteration, a list is a bit faster than an array.
答案3
得分: 0
import numpy as np
from collections import defaultdict
arr = np.array([[1,2,3,3],[5,6,7,8],[8,7,6,5]]).T
# 字典中每个键的每个值都是一个列表
d = defaultdict(list)
for a in arr:
d[a[0]].append((a[1], a[2]))
print(d)
英文:
import numpy as np
from collections import defaultdict
arr = np.array([[1,2,3,3],[5,6,7,8],[8,7,6,5]]).T
# every value of the key in your dict is a list
d = defaultdict(list)
for a in arr:
d[a[0]].append((a[1], a[2]))
print(d)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论