are elements of a list in sequence in list b

Matthew_WARREN at bnpparibas.com Matthew_WARREN at bnpparibas.com
Fri Feb 8 11:39:55 EST 2008


Hallo,


I need to search list a for the sequence of list b

First I went
>>> a=[1,2,34,4,5,6]
>>> b=[2,3,4]
>>> a in b
False

So

''.join([ v.__str__() for v in b ]) in ''.join([ v.__str__() for v in a ])

>>> s=SomeObject()
>>> a=[1,2,3,[s,3,[4,s,9]],s,4]
>>> b=[3,[s,3,[4,s,9]],s,4]
>>> ''.join([ v.__str__() for v in b ]) in ''.join([ v.__str__() for v in a
])
True


Was my second guess. I thought about set() but it doesn't see element order
as important.

That feels like a kludge to me, but makes python do the work of searching.

I know there are fast algorithmic ways of achieving it by hand, but my
lists are only 20 to 30 elements long, and I will be making max 100
searches at a time, probably about once every 2 or 3 seconds.

So speed not greatly important, A feeling I've written good code and taken
a good approach is. I've hunted for a recipe but couldn't find one to look
at and see how it was done.

Matt.
--
--


This message and any attachments (the "message") is
intended solely for the addressees and is confidential. 
If you receive this message in error, please delete it and 
immediately notify the sender. Any use not in accord with 
its purpose, any dissemination or disclosure, either whole 
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message. 
BNP PARIBAS (and its subsidiaries) shall (will) not 
therefore be liable for the message if modified. 
Do not print this message unless it is necessary,
consider the environment.

                ---------------------------------------------

Ce message et toutes les pieces jointes (ci-apres le 
"message") sont etablis a l'intention exclusive de ses 
destinataires et sont confidentiels. Si vous recevez ce 
message par erreur, merci de le detruire et d'en avertir 
immediatement l'expediteur. Toute utilisation de ce 
message non conforme a sa destination, toute diffusion 
ou toute publication, totale ou partielle, est interdite, sauf 
autorisation expresse. L'internet ne permettant pas 
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce 
message, dans l'hypothese ou il aurait ete modifie.
N'imprimez ce message que si necessaire,
pensez a l'environnement.



More information about the Python-list mailing list