Help doing it the "python way"

S

Scott Siegler

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!
 
P

Paul Rubin

Scott Siegler said:
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))
 
N

Nobody

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.
 
J

Jan Kuiken

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]])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top