Tuple in a list

This is the place for queries that don't fit in any of the other categories.

Tuple in a list

Postby Lev » Thu Mar 07, 2013 11:19 pm

Hi people,I am new here.I hope you can help me with the following :

This is a part of the code I am writing now in the 3.3 version of python.I have the "E" list,containing tuples of 2 elements(co ordinates of some points in a 2D level) and after I do some sorting I am trying to take the "y co-ordinate" in the first case,and the "x co-ordinate" in the second case,to create a center of the points in the 2D level.
I use turtle graphics to show this "new centre point" but sometimes it does not fit right in the image.Am I doing something wrong in the code or in the math I use?

Code: Select all
sorted_E_by_y = sorted(E, key=lambda tup: tup[1])
lowest_py = (sorted_E_by_y[0])[1]
highest_py = (sorted_E_by_y[length_of_E-1])[1]
sorted_E_by_x = sorted(E, key=lambda tup: tup[0])
lowest_px = (sorted_E_by_x[0])[0]
highest_px = (sorted_E_by_x[length_of_E-1])[0]
centre_x = (highest_px-lowest_px)/2
centre_y = (highest_py-lowest_py)/2

Is there another way to calculate this plasmatic centre of the many points in a 2d level?
Last edited by micseydel on Thu Mar 07, 2013 11:41 pm, edited 1 time in total.
Reason: Added code tags; OP, please do so yourself in the future.
Posts: 1
Joined: Thu Mar 07, 2013 10:41 pm

Re: Tuple in a list

Postby micseydel » Thu Mar 07, 2013 11:42 pm

Please read the following: viewtopic.php?f=6&t=145
Join the #python-forum IRC channel on irc.freenode.net for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from :)
User avatar
Posts: 2918
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Tuple in a list

Postby ichabod801 » Fri Mar 08, 2013 1:18 am

You need to add in your last two calculations, not subtract. Currently you're calculating have the different, not the midpoint.
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, C++
Previous serious languages: Erlang, R, Java, VBA, Lisp, HyperTalk, BASIC
Posts: 643
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 5 guests