Creating a List from a Loop

Chris Angelico rosuav at gmail.com
Sun Feb 14 19:57:43 EST 2016


On Mon, Feb 15, 2016 at 11:23 AM, Kaley Tucker <kaleywall93 at gmail.com> wrote:
> I'm in an Intro to Python class and have an assignment where I need to implement a function calculating distance between two points, then create a loop to find points within a certain distance and create a list from those points. I was able to create the function, but I cant get my loop to create the list. Any help would be appreciated. I can't figure out the solution from my textbooks or other web sources.
>
> my current work:
>
> https://ghostbin.com/paste/qypvb

Hi Kaley! Welcome to python-list; you're learning one of the best
languages in the world.

When you have questions like this, it's usually best to include your
source code right in the body of the email, rather than including a
link or an attachment. Attachments often don't survive, and links can
break (plus, some people will simply refuse to click on them). Here's
the code:

import math
blue_points = [[ 30 , 536254.99137 , 3659453.06343 ], [ 33 ,
536721.584912 , 3659162.97207 ], [ 50 , 535807.099324 , 3659576.92825
], [ 112 , 536827.131371 , 3657913.01245 ], [ 117 , 536473.254082 ,
3659433.57702 ], [ 120 , 536196.9844 , 3658713.72722 ], [ 127 ,
536387.547701 , 3658527.70015 ], [ 133 , 537397.838429 , 3659554.48657
], [ 144 , 537715.931243 , 3658625.59997 ], [ 166 , 538367.648437 ,
3658867.34288 ], [ 172 , 537112.662366 , 3657921.28957 ], [ 173 ,
536418.315024 , 3658715.47946 ], [ 209 , 538096.28422 , 3658514.93514
], [ 211 , 538077.87716 , 3658138.39337 ], [ 223 , 536220.396985 ,
3659243.54161 ], [ 242 , 536102.087002 , 3658703.61054 ], [ 244 ,
536968.755886 , 3659409.42857 ], [ 246 , 535996.903591 , 3658705.08691
], [ 275 , 538078.165429 , 3659022.35547 ], [ 303 , 535999.885405 ,
3658521.91524 ]]
red_point = [1, 1073706.744,3658967.925] #points [ID, x coordinate, y
coordinate]
def identifyNeighbor(point, c_point):
    cid = c_point[0]
    cx = c_point[1]
    cy = c_point[2]
    bid = point[0]
    x = point[1]
    y = point [2]
    c_point = (cid, cx, cy)
    point = (bid, x, y)
    dx = cx - x
    dy = cy - y
    dsquared = dx**2 + dy**2
    edistance = math.sqrt(dsquared)
    if edistance <= 536000:
        print "True"
    else:
        print "False"
neighbor = []
def neighborloop(p):
    while p <= 18:
        p = p + 1
        identifyNeighbor(blue_points[p], red_point)
        if identifyNeighbor == "True":
            neighbor.append(blue_points[p][0])


What you've done here is make a function which *prints* the word
"True" or "False". Instead, you want to *return* something. Check your
class textbook or other course materials; you should have been
introduced to this concept. Once you figure that part out, you'll
probably want to do the same thing in neighborloop too; don't use a
global list, but instead, create a new list inside the function, and
return that list. I'll let you explore the details on your own, rather
than giving it all away (where's the fun in that!).

Incidentally, your print statements indicate that you're using Python
2 here. I strongly recommend using Python 3 instead; the differences
aren't huge, but it's much easier if you start on the modern side of
the fence, instead of the backward-compatibility mode. There are a few
small syntactic changes, but the most significant change involves
Unicode handling, and trust me, it's a LOT easier if you learn that
correctly from the beginning, rather than going through ASCII ->
Extended ASCII -> DBCS -> Extended ASCII -> Unicode, the way I did!

Nothing in your code actually calls any of these functions. Is that
how you intend to do this?

Any time you have a 'while' loop that counts upward, have a look to
see if you can do this as a 'for' loop instead. Again, I'm assuming
you've been taught about them already; if you haven't, ignore this
recommendation, and just know that the way you're looping is wordy and
unnecessary :)

Have fun learning Python. It's a great language, and you seem to have
most of it sorted out. If you have more trouble, don't hesitate to
come back to this list, or to the python-tutor list if you think your
problems are more at the tutorial level than general. All the best!

ChrisA


More information about the Python-list mailing list