Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading