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

1.5k Views Asked by At

So, I've been using the set method "symmetric_difference" between 2 ndarray matrices in the following way:

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])

this method works fine for me, but it feel a little clumsy...is there anyway to conduct this in a slightly more elegant way?

2

There are 2 best solutions below

1
On BEST ANSWER

A simple list of tuples:

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

put it in a set:

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

np.array just wraps that set in an object dtype:

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

but make it into a list, and get a 2d array:

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

Since it is a list of tuples, it can also be made into a structured array:

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

If the tuples varied in length, the list of tuples would be turned into a 1d array of tuples (object dtype):

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,)
1
On

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.
[...]

There is nothing wrong with your code. Though if I had to code review it, I would suggest the following

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])