将一组元组转换为 python 中的 numpy 列表数组

convert a set of tuples into a numpy array of lists in python

所以,我一直在通过以下方式在 2 个 ndarray 矩阵之间使用 set 方法 "symmetric_difference":

x_set = list(set(tuple(i) for i in x_spam_matrix.tolist()).symmetric_difference(
                 set(tuple(j) for j in partitioned_x[i].tolist())))

x = np.array([list(i) for i in x_set])

这个方法对我来说很好用,但感觉有点笨拙...有没有更优雅的方式来进行这个?

The Zen of Python:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
[...]

您的代码没有任何问题。 虽然如果我必须对其进行代码审查,我会建议以下内容

spam_matrix_set = set(tuple(item) for item in x_spam_matrix.tolist())
partitioned_set = set(tuple(item) for item in partitioned_x[index].tolist())
disjunctive_union = spam_matrix_set.symmetric_difference(partitioned_set)

x = np.array([list(item) for item in disjunctive_union])

一个简单的元组列表:

In [146]: alist = [(1,2),(3,4),(2,1),(3,4)]

放在一组中:

In [147]: aset = set(alist)
In [148]: aset
Out[148]: {(1, 2), (2, 1), (3, 4)}

np.array 只是将设置包装在对象数据类型中:

In [149]: np.array(aset)
Out[149]: array({(1, 2), (3, 4), (2, 1)}, dtype=object)

但是把它变成一个列表,得到一个二维数组:

In [150]: np.array(list(aset))
Out[150]: 
array([[1, 2],
       [3, 4],
       [2, 1]])

既然是元组列表,也可以做成结构化数组:

In [151]: np.array(list(aset),'i,f')
Out[151]: array([(1, 2.), (3, 4.), (2, 1.)], dtype=[('f0', '<i4'), ('f1', '<f4')])

如果元组的长度不同,元组列表将变成一维元组数组(对象数据类型):

In [152]: np.array([(1,2),(3,4),(5,6,7)])
Out[152]: array([(1, 2), (3, 4), (5, 6, 7)], dtype=object)
In [153]: _.shape
Out[153]: (3,)