【发布时间】:2021-07-13 18:30:49
【问题描述】:
我有两个由两组元组组成的 numpy 数组:
a = [(1, "alpha"), (2, 3), ...]
b = [(1, "zylo"), (1, "xen"), (2, "potato", ...]
元组中的第一个元素是标识符并在两个数组之间共享,所以我想创建一个新的numpy数组,如下所示:
[(1, "alpha", "zylo", "xen"), (2, 3, "potato"), etc...]
我目前的解决方案有效,但对我来说效率太低了。看起来像这样:
aggregate_collection = []
for tuple_set in a:
for tuple_set2 in b:
if tuple_set[0] == tuple_set2[0] and other_condition:
temp_tup = (tuple_set[0], other tuple values)
aggregate_collection.append(temp_tup)
我怎样才能有效地做到这一点?
【问题讨论】:
-
我怀疑
pandas.group_by的答案只是隐藏了复杂性。你确定这些是numpy数组吗?它们看起来更像元组列表。这不是从numpy中受益的任务 -
@hpaulj 这些是带有单个元组元素的 numpy 数组。
-
我同意
pandas看起来不是正确的方法。看起来numpy也不是正确的数据结构,因为您将多种类型混合在一起。我认为这个minimal reproducible example 并不代表实际问题:p
标签: python arrays pandas numpy