[Tutor] Comparing lists

Jonas Geiregat kemu@linuxmail.org
Wed, 10 Jul 2002 00:21:53 +0800


someting like this will work:


for i in lista:
   for x in listb:
       if x == i: 
           print x is in lista AND listb
       

----- Original Message -----
From: Ashish <asis@graffiti.net>
Date: Tue, 09 Jul 2002 20:50:18 +0545 
To: tutor@python.org
Subject: Re: [Tutor] Comparing lists


> Terje Johan Abrahamsen wrote:
> > I have two lists of different length. Actually each list can differ in 
> > lenght every time. For example lista = [12, 34, 56] & listb = [14, 16, 
> > 34, 81]. What I now need to do is to check if the lists have one or more 
> > common numbers. In this case 34 exists in each list. Then I would like 
> > the function to somehow indicate this. Does it exist a simple way to do 
> > this? I have made a somehow functioning function that does this, but it 
> > is probably about 50-60 lines. (If lista[0] == listb[0], if lista[1] == 
> > listb[0] and so forth....)
> > 
> 
> 
> may be
> common = []
> for i in lista:
>      if i in listb:
>          common.append(i)
> 
> 
> 
> -- 
> Ashish Shrestha
> Gha 2-482, Balajutar, Kathmandu, Nepal
> Phone: 977-1-350593.
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Get your free email from www.linuxmail.org 


Powered by Outblaze