Pythonic way to convert a dictionary to a numpy array

dictionary, numpy, python

Solution

Try a pandas Series, it was built for this.

import pandas as pd
s = pd.Series({'a':1, 'b':2, 'c':3})
s.values # a numpy array

Problem

This is more of a question about programming style. I scrap webpages for fields such as: "Temperature: 51 - 62", "Height: 1000-1500"...etc The results are saved in a dictionary ``` {"temperature": "51-62", "height":"1000-1500" ...... } ``` All key and values are string type. Every key can map to one of many possible values. Now I want to convert this dictionary to numpy array/vector. I have the following concerns: - Each key corresponds to one index position in the array. - Each possible string value is mapped to one integer. - For some dictionary, some keys are not available. For example, I also have a dictionary that has no "temperature" key, because that webpage doesn't contain such field. I am wondering what is the most clear and efficient way of write such a conversion in Python. I am thinking of building another dictionary maps the key to the index number of the vector. And many other dictionaries that maps the values to integers. Another problem I am having is I am not sure about the range of some keys. I want to dynamically keep track of the mapping between string values and integers. For example, I may find that key1 can map to a val1_8 in the future. Thanks

Original source

Related problems