Help doing it the "python way"

Duncan Booth duncan.booth at invalid.invalid
Thu May 24 16:53:46 EDT 2012


Scott Siegler <scott.siegler at gmail.com> wrote:
> Hello,
>> I am an experienced programmer but a beginner to python. As such, I
> can figure out a way to code most algorithms using more "C" style
> syntax. 
>> I am doing something now that I am sure is a more python way but i
> can't quite get it right. I was hoping someone might help. 
>> So I have a list of grid coordinates (x, y). From that list, I want
> to create a new list that for each coordinate, I add the coordinate
> just above and just below (x,y+1) and (x,y-1) 
>> right now I am using a for loop to go through all the coordinates and
> then separate append statements to add the top and bottom. 
>> is there a way to do something like: [(x,y-1), (x,y+1) for zzz in
> coord_list] or something along those lines? 
>> thanks!
>
def vertical_neighbours(coords):
 for x, y in coords:
 yield x, y-1
 yield x, y+1
new_coords = list(vertical_neighbours(coords))
-- 
Duncan Booth http://kupuguy.blogspot.com


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /