Home > Mobile >  Joining two lists without losing data points - python
Joining two lists without losing data points - python

Time:04-20

I have a Data set with information regarding angles and distances. When tryin to plot the angles vs distances, I realised that the points were not "organized". So, I tried to join the two lists (Angles and Distances) in a dictionary and then organize the dictionary by key. in the end, I defined two new lists and attributed to one the keys, and to the other the values.

Like This:

x = dict(zip(angle, dist))
new_dict = {}
y = sorted(x.keys())

for i in y:
     new_dict[i] = x[i]

sorted_angle = new_dict.keys()
sorted_dist = new_dict.values()

The issue I'm facing is the fact that the length of the original angle list is quite different than the length of sorted_angle.

Am I losing Data Points here? And if so, how can I correct it?

Thank you all!

CodePudding user response:

Looks like you have duplicate values in the angle list, which is the reason why you are seeing the size reduce. The keys of a dictionary have to be unique. If all you wish to do is sort both the lists by angle, you could instead do this:

sorted_dist = [x for _, x in sorted(zip(angle, dist))]
sorted_angle = sorted(angle)
  • Related