I have a piece of text data, indexed with blocks, ie, organised by [block time obs1 obs2]
There are 50 blocks, and each block has 100 lines of data. So the text data is in the size of 5000 * 4. I would like to select some blocks of data and etablish a dictionary for these selected data, ie., the selected blocks indices are the keys, and for each key the arrays [time obs1, obs2] are the values.
I first defined the data array: data = array([5000*4]) and the selected keys,for example, allkeys = ['0', '1', '2']
Then I try to create a dictionary for the selected data:
data_blks={}
for i in range(len(allkeys)):
temp_key = allkeys[i]
data_blks[temp_key] = data[where(data[:,1] == int(temp_key))]
This can produce the key and values correctly in most cases. But it does not work for some cases, for example, if the allkeys are defined as ['40', '41']. In this case, the output data_blks has the keys ['41','40'] with the order not correct ! I tested the output of each line and it seems the line
data_blks[temp_key] = data[where(data[:,1] == int(temp_key))]
twisted the key order. But I find no reason why it would do so ! (it seems to me the dictionary in python is still under development and need improvements in its flexibility !)
To solve this problem, I need develop a method that I can insert the key and its values properly. Before I do so, I found a useful tool http://www.voidspace.org.uk/python/odict.html#downloading. It has a method call "insert", which allows me to insert the index, the key and its values ine one simple line: data_blks.insert(index, key, values).
I imported this module into my code and redefined the dictionary as
data_blks = OrderedDict([])
Then in the for loop, i just insert the key and values for each index, and it works beautifully and the previous problem is now gone !
I am still wondering why the previous (classic) method does not work properly ....
Aucun commentaire:
Enregistrer un commentaire