Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

looping through a list of lists.

5 views
Skip to first unread message

saoirse_79

unread,
Oct 8, 2003, 11:04:28 AM10/8/03
to
I have a list of lists as follows:
[['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'],
['-', '-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'],
['T', 'L', 'E', 'E', 'L', 'M', 'K', 'L', 'S', 'P', 'E']]
I want to be able to read each character and compare it with all
characters at the same positon in all sublists. Is this possible.
I have tried a few different methods but nothing seems to allow me to
compare the sublists character by character.


SBru...@trisystems.co.uk

unread,
Oct 8, 2003, 11:13:24 AM10/8/03
to
> From: saoirse_79 [SMTP:saoir...@yahoo.com]
PythonWin 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on
win32.
Portions Copyright 1994-2001 Mark Hammond (mham...@skippinet.com.au) - see
'Help/About PythonWin' for further copyright information.
>>> spam = [['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'],['-',

'-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'], ['T', 'L', 'E', 'E', 'L',
'M', 'K', 'L', 'S', 'P', 'E']]
>>> for eggs, chips, beans in zip(*spam):
... print eggs, chips, beans
...
- - T
- - L
- - E
- - E
- L L
- Q M
- Q K
K T L
S N S
A S P
K E E

HTH...

Cheers,
Simon Brunning,
http://www.brunningonline.net/simon/blog/
--LongSig


-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.

Rob Hunter

unread,
Oct 8, 2003, 12:13:08 PM10/8/03
to
The responder to this post has a cool way of doing it, but it won't
work (I believe) if you have an arbitrary number of sublists. Can you
clarify the problem? Do you have an arbitrary number of sublists? Or
is it always 3?

Rob

On Wednesday, October 8, 2003, at 11:04 AM, saoirse_79 wrote:

> I have a list of lists as follows:
> [['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'],
> ['-', '-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'],
> ['T', 'L', 'E', 'E', 'L', 'M', 'K', 'L', 'S', 'P', 'E']]
> I want to be able to read each character and compare it with all
> characters at the same positon in all sublists. Is this possible.
> I have tried a few different methods but nothing seems to allow me to
> compare the sublists character by character.
>
>
>
>

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


Andy Todd

unread,
Oct 8, 2003, 12:29:57 PM10/8/03
to
SBru...@trisystems.co.uk wrote:
>>From: saoirse_79 [SMTP:saoir...@yahoo.com]

>>I have a list of lists as follows:
>>[['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'],
>>['-', '-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'],
>>['T', 'L', 'E', 'E', 'L', 'M', 'K', 'L', 'S', 'P', 'E']]
>>I want to be able to read each character and compare it with all
>>characters at the same positon in all sublists. Is this possible.
>>I have tried a few different methods but nothing seems to allow me to
>>compare the sublists character by character.
>
>
> PythonWin 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on
> win32.
> Portions Copyright 1994-2001 Mark Hammond (mham...@skippinet.com.au) - see
> 'Help/About PythonWin' for further copyright information.
>
>>>>spam = [['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'],['-',

>
> '-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'], ['T', 'L', 'E', 'E', 'L',
> 'M', 'K', 'L', 'S', 'P', 'E']]
>
>>>>for eggs, chips, beans in zip(*spam):
>
> ... print eggs, chips, beans
> ...
> - - T
> - - L
> - - E
> - - E
> - L L
> - Q M
> - Q K
> K T L
> S N S
> A S P
> K E E
>
> HTH...
>
> Cheers,
> Simon Brunning,
> http://www.brunningonline.net/simon/blog/
> --LongSig
>

OK Simon, that is pretty evil. Please explain to a poor ignoramous the
difference between zip(spam) and zip(*spam).

I can see that the first gives;

[(['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'],),
(['-','-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'],),
(['T', 'L', 'E', 'E', 'L','M', 'K', 'L', 'S', 'P', 'E'],)]

and the second produces the result you illustrate above, but why?

Regards,
Andy
--
--------------------------------------------------------------------------------
From the desk of Andrew J Todd esq - http://www.halfcooked.com/

Peter Otten

unread,
Oct 8, 2003, 1:15:44 PM10/8/03
to
Rob Hunter wrote:

> The responder to this post has a cool way of doing it, but it won't
> work (I believe) if you have an arbitrary number of sublists. Can you
> clarify the problem? Do you have an arbitrary number of sublists? Or
> is it always 3?

I you are referring to the zip trick posted by SBru...@trisystems.co.uk,
it *does work for an arbitrary number of sublists, but not for
arbitrary-length sublists:

>>> for loc in zip(*"alpha beta gamma delta".split()):
... print loc
...
('a', 'b', 'g', 'd')
('l', 'e', 'a', 'e')
('p', 't', 'm', 'l')
('h', 'a', 'm', 't')
>>>

Peter

anton muhin

unread,
Oct 8, 2003, 1:31:13 PM10/8/03
to
Peter Otten wrote:

Consider the following:

PythonWin 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32.


Portions Copyright 1994-2001 Mark Hammond (mham...@skippinet.com.au) -
see 'Help/About PythonWin' for further copyright information.

>>> l = [range(0, 5), range(1, 7), range(3, 5)]
>>> l
[[0, 1, 2, 3, 4], [1, 2, 3, 4, 5, 6], [3, 4]]
>>> for e in zip(*l):
... print e
...
(0, 1, 3)
(1, 2, 4)
>>> for e in map(None, *l): print e
...
(0, 1, 3)
(1, 2, 4)
(2, 3, None)
(3, 4, None)
(4, 5, None)
(None, 6, None)
>>>

It pads missing elements with Nones. Is it what you need?

zip(*l) is similar to zip(l[0], l[1], ...., l[len(l) - 1])---it
substitutes list elements to function parameters.

HTH,
anton.

Rob Hunter

unread,
Oct 8, 2003, 1:42:00 PM10/8/03
to
Oh. That's pretty cool actually.

Rob

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


SBru...@trisystems.co.uk

unread,
Oct 13, 2003, 8:17:22 AM10/13/03
to r...@cs.brown.edu, saoir...@yahoo.com, pytho...@python.org
> On Wednesday, October 8, 2003, at 11:04 AM, saoirse_79 wrote:

> I have a list of lists as follows:
> [['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'],
> ['-', '-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'],
> ['T', 'L', 'E', 'E', 'L', 'M', 'K', 'L', 'S', 'P', 'E']]
> I want to be able to read each character and compare it with all
> characters at the same positon in all sublists. Is this possible.
> I have tried a few different methods but nothing seems to allow me
to
> compare the sublists character by character.

> From: Rob Hunter [SMTP:r...@cs.brown.edu]
>
> The responder to this post has a cool way of doing it, but it won't
> work (I believe) if you have an arbitrary number of sublists. Can you
> clarify the problem? Do you have an arbitrary number of sublists? Or
> is it always 3?

It *can* work for arbitrary numbers of sublists, depending upon what one
means by 'comparing'. For example:

>>> spam = [['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'], ['-',
'-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'], ['T', 'L', 'E', 'E', 'L',
'M', 'K', 'L', 'S', 'P', 'E']]

>>> for sublist in zip(*spam):
... print sublist, max(sublist), min(sublist)
...
('-', '-', 'T') T -
('-', '-', 'L') L -
('-', '-', 'E') E -
('-', '-', 'E') E -
('-', 'L', 'L') L -
('-', 'Q', 'M') Q -
('-', 'Q', 'K') Q -
('K', 'T', 'L') T K
('S', 'N', 'S') S N
('A', 'S', 'P') S A
('K', 'E', 'E') K E
>>> spam.append(['1', '@', '_', 'Z', 'A', 'z', 'a', 'T', 'L', '5', '!'])
>>> for sublist in zip(*spam):
... print sublist, max(sublist), min(sublist)
...
('-', '-', 'T', '1') T -
('-', '-', 'L', '@') L -
('-', '-', 'E', '_') _ -
('-', '-', 'E', 'Z') Z -
('-', 'L', 'L', 'A') L -
('-', 'Q', 'M', 'z') z -
('-', 'Q', 'K', 'a') a -
('K', 'T', 'L', 'T') T K
('S', 'N', 'S', 'L') S L
('A', 'S', 'P', '5') S 5
('K', 'E', 'E', '!') K !

Cheers,
Simon Brunning,
http://www.brunningonline.net/simon/blog/
--LongSig

0 new messages