I need create a set of the IDs of some messages, and the positions in the original list. The code is used to sort the messages an later handle them according to ID.
The following works, is readable, but slow.
import numpy as np
IDs=np.array([354,45,45,34,354])#example, the actual array is huge
Dict={}
for counter in xrange(len(IDs)):
try:
Dict[IDs[counter]].append(counter)
except:
Dict[IDs[counter]]=[counter]
print(Dict)
#{354: [0, 4], 34: [3], 45: [1, 2]}
Any ideas how to speed it up? There is no need for the lists to be sorted. Later in the code is used as follows, and after that the dict is discarded
for item in Dict.values():
Position_of_ID=Position[np.array(item)]
...