I have a problem with Python dictionary performance.
I have to read a file that with many x,y location and if draw this point it looks like a circle , and i have to resize this circle by a specific number from the edge. If Mydict has over 50000 items, it becomes slow.
Mydict is like { (X,Y) : {value:1 ,value:2} ,}
Key is a tuple and value is another dict.
Mydict = get_data()
cut_num = 5 # cut size
# start remove x
max_group = tuple(map(max,*Mydict))
min_group = tuple(map(min,*Mydict))
X_max = max_group[0] - cut_num
X_min = min_group[0] + cut_num
remove_keys =[k for k in Mydict if k[0]> X_max or k[0]< X_min ]
for key in remove_keys:
del Mydict[key]
Remove X value is faster just about 1 second.
But I don't know how to remove Y value in a efficiency way.
I write a function to get Y Max,Min By X value:
def Get_Y_By_Xaxis(Mydict,X_value):
temp_dict = {k for k in Mydict if k[0] == X_value }
max_group = tuple(map(max,*temp_dict ))
min_group = tuple(map(min,*temp_dict ))
return (min_group[1],max_group[1])
And start to remove Y value. I do it like this
for i in range(X_min ,X_max+1 ):
limits = Get_Y_By_Xaxis(Mydict , i)
remove_keys =[k for k in Mydict
if (k[0] == i and k[1] > limits[0] - cut_num )
or (k[0] == i and k[1] < limits[1] + cut_num )]
for key in remove_keys:
del Mydict[key]
The will cost about 60~70 second for a dictionary with 150000 item
Is this code will become faster or do I have to use some package to help me?