【问题标题】:How to merge 2 arrays python (similar to SQL Join)如何合并2个数组python(类似于SQL Join)
【发布时间】:2020-01-13 22:55:24
【问题描述】:

我有 2 个大数组 (500,1,23000) 和另一个 (700,1,25000) 我需要合并它们。他们是不同的

简单的例子是这样的:

a = np.array([['a', 3, 5, 6, 9], ['b', 14, 15, 56]])
b = np.array([['b', 4, 76, 44, 91, 100], ['c', 14, 15],['d',2,6,7])

想要的结果:

c = [['a', 3, 5, 6, 9], ['b', 4, 76, 44, 91, 100],['c', 14, 15],['d', 2, 6, 7]]

这是机器学习数据预处理的一部分。

【问题讨论】:

  • 如果我复制粘贴你的b 表达式,我会得到一个object dtype数组;本质上是一个列表列表。这和numpy 有什么关系?这个'a' 列表来自哪里?到目前为止,您的问题应该以unclear 结束。
  • 数组“a”的第二个值和数组“b”的第一个值(前面有'b'的两个)的输出中的处理是故意的还是错字?

标签: python numpy machine-learning keras autoencoder


【解决方案1】:

这可能会更快(它迭代两个列表两次),但应该给你你想要的。

import numpy as np
from collections import defaultdict

a = np.array([['a', 3, 5, 6, 9], ['b', 14, 15, 56]])
b = np.array([['b', 4, 76, 44, 91, 100], ['c', 14, 15],['d',2,6,7]])

def dictify(arr):
    return defaultdict(lambda : [], {x[0]: x[1:] for x in arr})

d1 = dictify(a)
d2 = dictify(b)

new_keys = set.union(set(d1.keys()), set(d2.keys()))

ans = [[k] + d1[k] + d2[k] for k in new_keys]

ans 的值为:

[['d', 2, 6, 7], ['c', 14, 15], ['a', 3, 5, 6, 9], ['b', 14, 15, 56, 4, 76, 44, 91, 100]]

【讨论】:

    【解决方案2】:

    如果您有兴趣,请查看 HACK,看看您的模糊要求

    a = np.array([['a', 3, 5, 6, 9], None])
    b = np.array([['b', 4, 76, 44, 91, 100], ['c', 14, 15], ['d', 2, 6, 7]])
    c = np.append(a, b)
    d = np.delete(c, 1)
    

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 2014-04-30
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多