Reputation: 735
I have an array [[0, 1, 1], [1, 0, 0], [1, 1, 0], [0, 1, 1], [1, 0, 0]]
and I want to get this [[0], [1], [2], [0], [1]]
- so I want to specify all the same vectors with unique number.
I have an idea how to do it, but it is really ugly, pretty slow and not pythonic at all. How could I do it more pythonic and nicer?
def get_multiclass_list_nodes(y_):
_class = 0
prev_list = list()
new_y = list()
for el in y_:
el = el.astype('int32').tolist()
if el in prev_list:
index = -1
for i, val in enumerate(prev_list):
if val == el:
index = i + 1
break
new_y.append([index])
else:
_class += 1
prev_list.append(el)
new_y.append([_class])
return np.array(new_y)
Upvotes: 0
Views: 52
Reputation: 12022
This should work for you:
def map_lists_to_ints(list_of_lists): # Name this better
tuples = (tuple(l) for l in list_of_lists)
seen = {}
next_int = 0
for tup in tuples:
if tup not in seen:
seen[tup] = next_int
next_int += 1
yield seen[tup]
list_of_lists = [[0, 1, 1], [1, 0, 0], [1, 1, 0], [0, 1, 1], [1, 0, 0]]
result = list(map_lists_to_ints(list_of_lists))
print(result)
listified_result = [[x] for x in result]
print(listified_result)
Output:
[0, 1, 2, 0, 1]
[[0], [1], [2], [0], [1]]
Upvotes: 2
Reputation: 8071
Maybe this is abusing numpy a bit, but an easy way:
a = np.array([[0, 1, 1], [1, 0, 0], [1, 1, 0], [0, 1, 1], [1, 0, 0]])
lst = [str(x) for x in a]
result = np.unique(lst, return_inverse=True)[1]
Perhaps you'll want to tweak the final result, but it's trivial from there.
Upvotes: 0