[Tutor] Tutor Digest, Vol 76, Issue 31

saurabh agrawal imsam100 at yahoo.co.in
Sat Jun 12 20:29:19 CEST 2010


Hi,

You can use method name count in list also for getting the repeats.

#!/usr/bin/python

li=[1,2,3,1,4,5,7,3,4,1]
for i in range(len(li)):
    print li[i], li.count(li[i])

cheers,

Saurabh



--- On Fri, 11/6/10, tutor-request at python.org <tutor-request at python.org> wrote:

From: tutor-request at python.org <tutor-request at python.org>
Subject: Tutor Digest, Vol 76, Issue 31
To: tutor at python.org
Date: Friday, 11 June, 2010, 8:19 PM

Send Tutor mailing list submissions to
    tutor at python.org

To subscribe or unsubscribe via the World Wide Web, visit
    http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
    tutor-request at python.org

You can reach the person managing the list at
    tutor-owner at python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."


Today's Topics:

   1. Looking for duplicates within a list (Ken G.)
   2. Re: Looking for duplicates within a list (Alex Hall)
   3. Re: Looking for duplicates within a list (Ken G.)
   4. Re: Looking for duplicates within a list (Ken G.)
   5. Re: Looking for duplicates within a list (Sander Sweers)
   6. Re: Looking for duplicates within a list (Jose Amoreira)


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

Message: 1
Date: Fri, 11 Jun 2010 09:57:34 -0400
From: "Ken G." <beachkid at insightbb.com>
To: tutor at python.org
Subject: [Tutor] Looking for duplicates within a list
Message-ID: <4C1240CE.7050807 at insightbb.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

I have been working on this problem for several days and I am not making 
any progress.  I have a group of 18 number, in ascending order, within a 
list.  They ranged from 1 to 39.  Some numbers are duplicated as much as 
three times or as few as none.

I started with one list containing the numbers.  For example, they are 
listed as like below:

a = [1, 2, 3, 3, 4]

I started off with using a loop:

    for j in range (0, 5):
    x = a[0] # for example, 1

How would I compare '1' with 2, 3, 3, 4? 

Do I need another duplicated list such as b = a and compare a[0] with 
either b[0], b[1], b[2], b[3], b[4]?

Or do I compare a[0] with a[1], a[2], a[3], a[4]?

In any event, if a number is listed more than once, I would like to know 
how many times, such as 2 or 3 times.  For example, '3' is listed twice 
within a list.

TIA,

Ken










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

Message: 2
Date: Fri, 11 Jun 2010 10:20:20 -0400
From: Alex Hall <mehgcap at gmail.com>
To: "Ken G." <beachkid at insightbb.com>
Cc: tutor at python.org
Subject: Re: [Tutor] Looking for duplicates within a list
Message-ID:
    <AANLkTilR4cZMiC1tsAMuTuzO8C32SO-qKDXe9spco681 at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On 6/11/10, Ken G. <beachkid at insightbb.com> wrote:
> I have been working on this problem for several days and I am not making
> any progress.  I have a group of 18 number, in ascending order, within a
> list.  They ranged from 1 to 39.  Some numbers are duplicated as much as
> three times or as few as none.
FYI, Python's "set" data type will let you have a list and never have
a repeat. I know that is not your goal now, but if you want to remove
duplicates, it seems like a good choice.
>
> I started with one list containing the numbers.  For example, they are
> listed as like below:
>
> a = [1, 2, 3, 3, 4]
>
> I started off with using a loop:
>
>     for j in range (0, 5):
>     x = a[0] # for example, 1
>
> How would I compare '1' with 2, 3, 3, 4?
>
> Do I need another duplicated list such as b = a and compare a[0] with
> either b[0], b[1], b[2], b[3], b[4]?
>
> Or do I compare a[0] with a[1], a[2], a[3], a[4]?
A couple points here. First, you will want to make life easier by
saying range(0, len(a)) so that the loop will work no matter the size
of a.
Second, for comparing a list to itself, here is a rather inefficient,
though simple, way:

for i in range(0, len(a)):
 x=a[i]
 for j in range(0, len(a)):
  y=a[j]
  if(x==y and i!=j): #match since a[i]==a[j] and i and j are not the
same index of a
>
> In any event, if a number is listed more than once, I would like to know
> how many times, such as 2 or 3 times.  For example, '3' is listed twice
> within a list.
Do not quote me here, but I think sets may be able to tell you that as well.
>
> TIA,
>
> Ken
>
>
>
>
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap


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

Message: 3
Date: Fri, 11 Jun 2010 10:31:58 -0400
From: "Ken G." <beachkid at insightbb.com>
To: vnbang2003 at yahoo.com, tutor at python.org
Subject: Re: [Tutor] Looking for duplicates within a list
Message-ID: <4C1248DE.6090508 at insightbb.com>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"

vijay wrote:
> Check out this code
>  l= [1, 2, 3, 3, 4]
>  d={}
>  for item in l:
>      d.setdefaut(item,0)
>      d[item] +=1
> print d
> {1: 1, 2: 1, 3: 2, 4: 1}
>
>
> with regard's
> vijay
>
>
Thanks.  Very interesting concept.

Ken
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100611/12e042f9/attachment-0001.html>

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

Message: 4
Date: Fri, 11 Jun 2010 10:40:58 -0400
From: "Ken G." <beachkid at insightbb.com>
To: Alex Hall <mehgcap at gmail.com>
Cc: tutor at python.org
Subject: Re: [Tutor] Looking for duplicates within a list
Message-ID: <4C124AFA.7030007 at insightbb.com>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"

Alex Hall wrote:
> On 6/11/10, Ken G. <beachkid at insightbb.com> wrote:
>   
>> I have been working on this problem for several days and I am not making
>> any progress.  I have a group of 18 number, in ascending order, within a
>> list.  They ranged from 1 to 39.  Some numbers are duplicated as much as
>> three times or as few as none.
>>     
> FYI, Python's "set" data type will let you have a list and never have
> a repeat. I know that is not your goal now, but if you want to remove
> duplicates, it seems like a good choice.
>   
>> I started with one list containing the numbers.  For example, they are
>> listed as like below:
>>
>> a = [1, 2, 3, 3, 4]
>>
>> I started off with using a loop:
>>
>>     for j in range (0, 5):
>>     x = a[0] # for example, 1
>>
>> How would I compare '1' with 2, 3, 3, 4?
>>
>> Do I need another duplicated list such as b = a and compare a[0] with
>> either b[0], b[1], b[2], b[3], b[4]?
>>
>> Or do I compare a[0] with a[1], a[2], a[3], a[4]?
>>     
> A couple points here. First, you will want to make life easier by
> saying range(0, len(a)) so that the loop will work no matter the size
> of a.
> Second, for comparing a list to itself, here is a rather inefficient,
> though simple, way:
>
> for i in range(0, len(a)):
>  x=a[i]
>  for j in range(0, len(a)):
>   y=a[j]
>   if(x==y and i!=j): #match since a[i]==a[j] and i and j are not the
> same index of a
>   
>> In any event, if a number is listed more than once, I would like to know
>> how many times, such as 2 or 3 times.  For example, '3' is listed twice
>> within a list.
>>     
> Do not quote me here, but I think sets may be able to tell you that as well.
>   
>> TIA,
>>
>> Ken
>>     
Thank you for your contribution.  As seen here, I have much to learn.

Ken

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100611/eacd401a/attachment-0001.html>

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

Message: 5
Date: Fri, 11 Jun 2010 16:46:25 +0200
From: Sander Sweers <sander.sweers at gmail.com>
To: "Ken G." <beachkid at insightbb.com>
Cc: tutor at python.org
Subject: Re: [Tutor] Looking for duplicates within a list
Message-ID:
    <AANLkTikGt6zifb0gOCSOR5vnvxg2MtFdaWJjr5RHBrb4 at mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On 11 June 2010 15:57, Ken G. <beachkid at insightbb.com> wrote:
> In any event, if a number is listed more than once, I would like to know how
> many times, such as 2 or 3 times. ?For example, '3' is listed twice within a
> list.

If you do not have top keep the order of the number this will work.

>>> a = [1, 2, 3, 3, 4]
>>> counted = {}
>>> for n in a:
    if not n in counted:
        counted[n] = 1
    else:
        counted[n] += 1

>>> counted
{1: 1, 2: 1, 3: 2, 4: 1}

>>> for x, y in counted.items():
    if y > 1:
        print "Number %s was found %s times" % (x, y)
    else:
        print "Number %s was found %s time" % (x, y)

Number 1 was found 1 time
Number 2 was found 1 time
Number 3 was found 2 times
Number 4 was found 1 time

Greets
Sander


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

Message: 6
Date: Fri, 11 Jun 2010 15:49:22 +0100
From: Jose Amoreira <ljmamoreira at gmail.com>
To: tutor at python.org
Subject: Re: [Tutor] Looking for duplicates within a list
Message-ID: <201006111549.22751.ljmamoreira at gmail.com>
Content-Type: Text/Plain;  charset="iso-8859-1"

On Friday, June 11, 2010 02:57:34 pm Ken G. wrote:
> I have been working on this problem for several days and I am not making
> any progress.  I have a group of 18 number, in ascending order, within a
> list.  They ranged from 1 to 39.  Some numbers are duplicated as much as
> three times or as few as none.
> 
> I started with one list containing the numbers.  For example, they are
> listed as like below:
> 
> a = [1, 2, 3, 3, 4]
> 
> I started off with using a loop:
> 
>     for j in range (0, 5):
>     x = a[0] # for example, 1
> 
> How would I compare '1' with 2, 3, 3, 4?
> 
> Do I need another duplicated list such as b = a and compare a[0] with
> either b[0], b[1], b[2], b[3], b[4]?
> 
> Or do I compare a[0] with a[1], a[2], a[3], a[4]?
> 
> In any event, if a number is listed more than once, I would like to know
> how many times, such as 2 or 3 times.  For example, '3' is listed twice
> within a list.
> 
> TIA,
>

I would do it with a dictionary:
def reps(lst):
    dict = {}
    for item in lst:
        if item in dict:
            dict[item] += 1
        else:
            dict[item] = 1
    return dict

This function returns a dictionary with of the number of times each value in 
the list is repeated. Even shorter using dict.setdefault:

def reps(lst):
    dict={}
    for item in lst:
        dict[item] = dict.setdefault(item,0) + 1
    return dict

For instance, if lst=[1,2,2,2,4,4,5], then reps(lst) returns
{1: 1, 2: 3, 4: 2, 5: 1}

Using the fact that the list is ordered, one can design a more efficient 
solution (go through the list; if this item is equal to the previous, then it 
is repeated, else, it is a new value). But you list is short enough for this 
direct approach to work.
Hope this helps. Cheers,
Jose


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

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


End of Tutor Digest, Vol 76, Issue 31
*************************************


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100612/80c6cb47/attachment-0001.html>


More information about the Tutor mailing list