如何合并2个数组python(类似于SQL Join)
How to merge 2 arrays python (similar to SQL Join)
我有 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]]
这是机器学习数据预处理的一部分。
这里有一个 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)
这可能会变得更快(它遍历两个列表两次),但应该给你你想要的。
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 个大数组 (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]]
这是机器学习数据预处理的一部分。
这里有一个 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)
这可能会变得更快(它遍历两个列表两次),但应该给你你想要的。
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]]