I have a numpy array
[['5.1', '3.5', '1.4', '0.2', 'Setosa'],
['4.9', '3.0', '1.4', '0.2', 'Versicolor']]
How to convert it to a dictionary such that
{['5.1', '3.5', '1.4', '0.2']:'Setosa', ['4.9', '3.0', '1.4', '0.2']:'Versicolor'}
I have a numpy array
[['5.1', '3.5', '1.4', '0.2', 'Setosa'],
['4.9', '3.0', '1.4', '0.2', 'Versicolor']]
How to convert it to a dictionary such that
{['5.1', '3.5', '1.4', '0.2']:'Setosa', ['4.9', '3.0', '1.4', '0.2']:'Versicolor'}
The dictionary you're currently specifying is invalid, since the keys of your dictionaries cannot be list objects. However, you could change them to tuples. As tuples are immutable, they can be used as keys.
A dictionary comprehension that converts the sample case you have provided might look like this:
a = numpy.array([['5.1', '3.5', '1.4', '0.2', 'Setosa'],
['4.9', '3.0', '1.4', '0.2', 'Versicolor']])
b = {tuple(x[:-1]) : x[-1] for x in a}
It creates a tuple from the first n-1 elements of each list, and then assigns the last element as the value. The resulting dictionary looks like this:
{('5.1', '3.5', '1.4', '0.2'): 'Setosa', ('4.9', '3.0', '1.4', '0.2'): 'Versicolor'}