[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Loop in a loop?

Sacred Heart

1/17/2008 12:22:00 PM

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
24 Answers

cokofreedom

1/17/2008 12:35:00 PM

0

On Jan 17, 1:21 pm, Sacred Heart <scrd...@gmail.com> wrote:
> 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

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.

Sacred Heart

1/17/2008 1:39:00 PM

0

On Jan 17, 1:35 pm, cokofree...@gmail.com 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

Chris

1/17/2008 1:52:00 PM

0

On Jan 17, 2:35 pm, cokofree...@gmail.com wrote:
> On Jan 17, 1:21 pm, Sacred Heart <scrd...@gmail.com> wrote:
>
>
>
> > 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
>
> 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.

You could always pre-pad the lists you are using before using the zip
function, kinda like

def pad(*iterables):
max_length = 0
for each_iterable in iterables:
if len(each_iterable) > max_length: max_length =
len(each_iterable)
for each_iterable in iterables:
each_iterable.extend([None for i in xrange(0,max_length-
len(each_iterable))])

pad(array1, array2, array3)
for i in zip(array1, array2, array3):
print i

What you could also do is create an index to use for it.

for i in xrange(0, length_of_longest_list):
try: print array1[i]
except IndexError: pass
try: print array2[i]
except IndexError: pass

cokofreedom

1/17/2008 2:43:00 PM

0

On Jan 17, 2:52 pm, Chris <cwi...@gmail.com> wrote:
> On Jan 17, 2:35 pm, cokofree...@gmail.com wrote:
>
>
>
> > On Jan 17, 1:21 pm, Sacred Heart <scrd...@gmail.com> wrote:
>
> > > 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
>
> > 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.
>
> You could always pre-pad the lists you are using before using the zip
> function, kinda like
>
> def pad(*iterables):
> max_length = 0
> for each_iterable in iterables:
> if len(each_iterable) > max_length: max_length =
> len(each_iterable)
> for each_iterable in iterables:
> each_iterable.extend([None for i in xrange(0,max_length-
> len(each_iterable))])
>
> pad(array1, array2, array3)
> for i in zip(array1, array2, array3):
> print i
>
> What you could also do is create an index to use for it.
>
> for i in xrange(0, length_of_longest_list):
> try: print array1[i]
> except IndexError: pass
> try: print array2[i]
> except IndexError: pass

couldn't you just do something like

if len(array1) is not len(array2):
if len(array1) < len(array2):
max_length = len(array2) - len(array1)
array1.extend([None for i in xrange(0, max_length)])
elif len(array1) > len(array2):
max_length = len(array1) - len(array2)
array2.extend([None for i in xrange(0, max_length)])

for i in zip(array1, array2):
print i

Though my case only really works for these two, whereas yours can be
used on more than two lists. :)

Bruno Desthuilliers

1/17/2008 2:44:00 PM

0

Sacred Heart a écrit :
> On Jan 17, 1:35 pm, cokofree...@gmail.com 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?

<ot>
Please gentlemen: Python has no builtin type named 'array', so
s/array/list/g
</ot>


Just pad your shortest list.


cokofreedom

1/17/2008 2:58:00 PM

0

>
> > 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?
>
> <ot>
> Please gentlemen: Python has no builtin type named 'array', so
> s/array/list/g
> </ot>
>
> Just pad your shortest list.

I agree, but was merely showing how he would use the variables he had
given.

Duncan Booth

1/17/2008 3:20:00 PM

0

Chris <cwitts@gmail.com> wrote:

> You could always pre-pad the lists you are using before using the zip
> function, kinda like
>
> def pad(*iterables):
> max_length = 0
> for each_iterable in iterables:
> if len(each_iterable) > max_length: max_length =
> len(each_iterable)
> for each_iterable in iterables:
> each_iterable.extend([None for i in xrange(0,max_length-
> len(each_iterable))])
>
> pad(array1, array2, array3)
> for i in zip(array1, array2, array3):
> print i
>

Another option is to pad each iterator as it is exhausted. That way you
can use any iterators not just lists. e.g.

from itertools import cycle, chain

def paddedzip(*args, **kw):
padding = kw.get('padding', '')
def generate_padding():
padders = []
def padder():
if len(padders) < len(args)-1:
padders.append(None)
while 1:
yield padding
while 1:
yield padder()

return zip(*(chain(it, pad)
for (it, pad) in zip(args, generate_padding())))

for i in paddedzip(xrange(10), ['one', 'two', 'three', 'four'],
['a', 'b', 'c'], padding='*'):
print i

Bruno Desthuilliers

1/17/2008 4:39:00 PM

0

cokofreedom@gmail.com a écrit :
(snip)
> couldn't you just do something like
>
> if len(array1) is not len(array2):

*never* use the identity operator to test equality ! The fact that
CPython memoize small integers is an implementation detail, *not* a part
of the language specification.

> if len(array1) < len(array2):
> max_length = len(array2) - len(array1)
> array1.extend([None for i in xrange(0, max_length)])
> elif len(array1) > len(array2):
> max_length = len(array1) - len(array2)
> array2.extend([None for i in xrange(0, max_length)])


Never heard of the builtin max() function ?-)

def pad(*lists, **kw):
padding = kw.get('padding', None)
lists_lens = [len(alist) for alist in lists]
padlen = max(lists_lens)
return [
alist + ([padding] * (padlen - list_len))
for list_len, alist in zip(lists_lens, lists)
]

for i in zip(*pad(range(3), range(5, 10))):
print i


Now there are very certainly smart solutions using itertools, but the
one I cooked is way too ugly so I'll leave this to itertools masters !-)

sturlamolden

1/17/2008 5:13:00 PM

0

On 17 Jan, 13:21, Sacred Heart <scrd...@gmail.com> wrote:

> A push in the right direction, anyone?

for number,letter in zip(array1,array2):
print "%s %s" % (number,letter)


sturlamolden

1/17/2008 5:21:00 PM

0

On 17 Jan, 14:38, Sacred Heart <scrd...@gmail.com> wrote:

> 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?

In that case your problem is the data. You'll either have to truncate
one array and/or pad the other.

Or is this what you want?

n = len(array1) if len(array1) < len(array2) else len(array2)
for number,letter in zip(array1[:n],array2[:n]):
print "%s %s" % (number,letter)
reminder = array1[n:] if len(array1) > len(array2) else array2[n:]
for x in reminder: print x