Loop in a loop?

2008-01-17 Thread Sacred Heart
Hi,
I'm new to Python and have come across a problem I don't know how to
solve, enter com.lang.python :)

I'm writing some small apps to learn the language, and I like it a lot
so far.

My problem I've stumbled upon is that I don't know how to do what I
want. I want to do a loop in a loop. I think.

I've got two arrays with some random stuff in, like this.

array1 = ['one','two','three','four']
array2 = ['a','b','c','d']

I want to loop through array1 and add elements from array2 at the end,
so it looks like this:

one a
two b
three c
four c

I'm stuck. I know how to loop through the arrays separatly and print
them, but both at the same time? Hmmm.

A push in the right direction, anyone?

R,
SH
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Loop in a loop?

2008-01-17 Thread Sacred Heart
On Jan 17, 1:35 pm, [EMAIL PROTECTED] wrote:
> for i in zip(array1, array2):
> print i
>
> Although I take it you meant four d, the issue with this method is
> that once you hit the end of one array the rest of the other one is
> ignored.

Yes, small typo there.

Okey, so if my array1 is has 4 elements, and array2 has 6, it won't
loop trough the last 2 in array2? How do I make it do that?

R,
SH

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Tutorial.

2008-01-18 Thread Sacred Heart
On Jan 17, 11:30 pm, Rizwan <[EMAIL PROTECTED]> wrote:
> Hiya,
>
> I found one good website for python tutorial. just thought to share
> with community.
>
> Hope you all also like it..
>
>  http://python.objectis.net
>
> -MR

Thanks, looks like a nice collection of links. I've bookmarked the
page.

R,
SH
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Loop in a loop?

2008-01-18 Thread Sacred Heart
On Jan 17, 7:39 pm, Paul Rubin <http://[EMAIL PROTECTED]> wrote:
> Sacred Heart <[EMAIL PROTECTED]> writes:
> > array1 = ['one','two','three','four']
> > array2 = ['a','b','c','d']
>
> > I want to loop through array1 and add elements from array2 at the end,
> > so it looks like this:
>
> > one a
> > two b
> > three c
> > four c
>
> The "functional" style is to use the zip function that someone
> described.  The old-fashioned way is simply:
>
>n = len(array1)
>for i in xrange(n):
>print array1[i], array2[i]
>
> You can modify this in various ways if the lengths of the lists are
> not equal.  E.g.

Thank you Paul, and everybody else contributing with answers of
various complexity. Although a whole bunch of them was way too complex
for my simple problem, but that's ok.

I now know how to use map and zip, and also learned some tips and
tricks.

Thanks.

All the best,
SH
-- 
http://mail.python.org/mailman/listinfo/python-list