Is there a way to append new values to a list with out combining them into one list?

So to explain the whole point of my code,
I have thousands of points to be plot. The problem is for a given x value I have multiple y value so my plot doesn’t represent a function. To fix this I wanted to take the max y values for every x points.

test_list=[(257.4, -6.0), (654.3, -3.0),(257.4, -9.0),(754.3, -0.0),(354.3, -11.0),(257.4, -5.0),(454.3, -10.0)] # example of pairs of points to be plot
x, y = zip(*test_list)
d=[]
for h,j in test_list:
    if int(h)>256:
        d.append(j)
        plt.plot(int(h),max(d),".")
print(max(d))

The issue with this code is that it maps all points of x to a single y value and this is because taking the max value of the list d gives me 1 point. How can I take the max value of list, d, for every h value. So that for instance for h=257 I have a list that is different that for h=258 and so on for all values h>256

>Solution :

is this what you’re trying to achieve?

test_list=[(257.4, -6.0), (654.3, -3.0),(257.4, -9.0),(754.3, -0.0),(354.3, -11.0),(257.4, -5.0),(454.3, -10.0)] # example of pairs of points to be plot

points = dict()
for x,y in test_list:
    if x not in points or points[x]<y:
        points[x]=y

x,y = zip(*points.items())
plt.figure()
plt.plot(x, y, '.')
plt.show()

Leave a Reply