Re: Help doing it the python way
On 5/24/12 22:22 , Scott Siegler wrote: 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? If you have lot's of numerical data you can use the NumPy module (http://numpy.scipy.org/), your problem would reduce to something like this (copied from an IPython shell, could be shorter) Regards, Jan Kuiken In [1]: first_list = np.arange(0, 10).reshape((5,2)) In [2]: above = np.array([0,-1]) In [3]: below = np.array([0,+1]) In [4]: N,d = first_list.shape In [5]: second_list = np.empty((N*2,d)) In [6]: second_list[0::2] = first_list + above In [7]: second_list[1::2] = first_list + below In [8]: second_list Out[8]: array([[ 0., 0.], [ 0., 2.], [ 2., 2.], [ 2., 4.], [ 4., 4.], [ 4., 6.], [ 6., 6.], [ 6., 8.], [ 8., 8.], [ 8., 10.]]) In [9]: first_list Out[9]: array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]) -- http://mail.python.org/mailman/listinfo/python-list
Re: Help doing it the python way
On 24 May 2012 21:22, Scott Siegler scott.sieg...@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? AFAICS nobody's suggested yet the simple yet effective: new_list = [(x, y + i) for x, y in coord_list for i in (-1, 1)] IMHO these list comprehensions are often overlooked too quickly in favour of itertools (in this case chain.from_iterable). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Help doing it the python way
On Thu, 24 May 2012 13:22:43 -0700, Scott Siegler wrote: is there a way to do something like: [(x,y-1), (x,y+1) for zzz in coord_list] or something along those lines? [(xx,yy) for x, y in coord_list for xx, yy in [(x,y-1),(x,y+1)]] or: [(x,yy) for x, y in coord_list for yy in [y-1,y+1]] Not to be confused with: [[(xx,yy) for xx, yy in [(x,y-1),(x,y+1)]] for x, y in coord_list] or: [[(x,yy) for yy in (y-1,y+1)] for x, y in coord_list] which will produce the same pairs in the same order but as a list of lists rather than as a single flat list. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help doing it the python way
On Thu, May 24, 2012 at 5:12 PM, Emile van Sebille em...@fenx.com wrote: On 5/24/2012 2:30 PM Paul Rubin said... Paul Rubinno.email@nospam.invalid writes: new_list = chain( ((x,y-1), (x,y+1)) for x,y in coord_list ) Sorry: new_list = list(chain( ((x,y-1), (x,y+1)) for x,y in coord_list)) from itertools import chain coord_list = zip(range(20,30),range(30,40)) a = [((x,y-1),(x,y+1)) for x,y in coord_list] b = list(chain(((x,y-1),(x,y+1)) for x,y in coord_list)) a == b True So, why use chain? Is it a premature optimization? Similar to the practice of using .join(targets) vs targeta+targetb+...+targetn? Paul's code is incorrect. It should use chain.from_iterable in place of chain. The difference then is that it creates a single list of coordinates, rather than a list of pairs of coordinates. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
Help doing it the python way
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! -- http://mail.python.org/mailman/listinfo/python-list
Re: Help doing it the python way
Scott Siegler scott.sieg...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Help doing it the python way
Scott Siegler scott.sieg...@gmail.com writes: is there a way to do something like: [(x,y-1), (x,y+1) for zzz in coord_list] or something along those lines? You should read the docs of the itertools module on general principles, since they are very enlightening in many ways. Your particular problem can be handled with itertools.chain: from itertools import chain new_list = chain( ((x,y-1), (x,y+1)) for x,y in coord_list ) You can alternatively write an iterative loop: def gen_expand(coord_list): for x,y in coord_list: yield (x-1,y) yield (x+1, y) new_list = list(gen_expand(coord_list)) -- http://mail.python.org/mailman/listinfo/python-list
Re: Help doing it the python way
On 05/24/2012 04:22 PM, Scott Siegler 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! So, where's the code that works? That you want optimized, or pythonified ? Your algorithm description is sufficiently confusing that I'm going to have make some wild guesses about what you're after. Apparently you have a list of tuples, and you want to create a second list that's related to the first in the sense that each item (i) of list2 is the sum of the items (i-1) and (i+1) of list1. Presumably you mean sum as in vector sum, where we add the x and y values, respectively. Easiest way to handle edge conditions is to stick an extra (0,0) at both the beginning and end of the list. list1 = [ (3,7), (2,2), (944, -2), (12, 12) ] def sumtuple(mytuple1, mytuple2): return ( mytuple1[0] + mytuple2[0] , mytuple1[1] + mytuple2[1]) def makesum(list1): list2 = [] list1a = [(0, 0)] + list1 + [(0, 0)] for item1, item2 in zip(list1a, list1a[2:]): list2.append( sumtuple(item1, item2) ) return list2 print makesum(list1) output: [(2, 2), (947, 5), (14, 14), (944, -2)] Now, that undoubtedly isn't what you wanted, but perhaps you could clarify the algorithm, so we could refine it. No point in making it more compact till it solves the problem you're actually interested in. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Help doing it the python way
Paul Rubin no.email@nospam.invalid writes: new_list = chain( ((x,y-1), (x,y+1)) for x,y in coord_list ) Sorry: new_list = list(chain( ((x,y-1), (x,y+1)) for x,y in coord_list)) -- http://mail.python.org/mailman/listinfo/python-list
Re: Help doing it the python way
On 5/24/2012 2:30 PM Paul Rubin said... Paul Rubinno.email@nospam.invalid writes: new_list = chain( ((x,y-1), (x,y+1)) for x,y in coord_list ) Sorry: new_list = list(chain( ((x,y-1), (x,y+1)) for x,y in coord_list)) from itertools import chain coord_list = zip(range(20,30),range(30,40)) a = [((x,y-1),(x,y+1)) for x,y in coord_list] b = list(chain(((x,y-1),(x,y+1)) for x,y in coord_list)) a == b True So, why use chain? Is it a premature optimization? Similar to the practice of using .join(targets) vs targeta+targetb+...+targetn? Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: Help doing it the python way
On 5/24/2012 4:53 PM, Duncan Booth wrote: Scott Sieglerscott.sieg...@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. Hi, welcome to Python. I came here from C also. 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) The Python way, especially the Python 3 way, is to not make the new sequence into a concrete list unless you actually need a list to append or sort or otherwise mutate. In many use cases, that is not necessary. 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? def vertical_neighbours(coords): for x, y in coords: yield x, y-1 yield x, y+1 new_coords = list(vertical_neighbours(coords)) -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list