<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Hi,<br><br>You can use method name count in list also for getting the repeats.<br><br>#!/usr/bin/python<br><br>li=[1,2,3,1,4,5,7,3,4,1]<br>for i in range(len(li)):<br> print li[i], li.count(li[i])<br><br>cheers,<br><br>Saurabh<br><br><br><br>--- On <b>Fri, 11/6/10, tutor-request@python.org <i><tutor-request@python.org></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: tutor-request@python.org <tutor-request@python.org><br>Subject: Tutor Digest, Vol 76, Issue 31<br>To: tutor@python.org<br>Date: Friday, 11 June, 2010, 8:19 PM<br><br><div class="plainMail">Send Tutor mailing list submissions to<br> <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br><br>To subscribe or unsubscribe via the World
Wide Web, visit<br> <a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>or, via email, send a message with subject or body 'help' to<br> <a ymailto="mailto:tutor-request@python.org" href="/mc/compose?to=tutor-request@python.org">tutor-request@python.org</a><br><br>You can reach the person managing the list at<br> <a ymailto="mailto:tutor-owner@python.org" href="/mc/compose?to=tutor-owner@python.org">tutor-owner@python.org</a><br><br>When replying, please edit your Subject line so it is more specific<br>than "Re: Contents of Tutor digest..."<br><br><br>Today's Topics:<br><br> 1. Looking for duplicates within a list (Ken G.)<br> 2. Re: Looking for duplicates within a list (Alex Hall)<br> 3. Re: Looking for duplicates within a list (Ken G.)<br> 4. Re: Looking for
duplicates within a list (Ken G.)<br> 5. Re: Looking for duplicates within a list (Sander Sweers)<br> 6. Re: Looking for duplicates within a list (Jose Amoreira)<br><br><br>----------------------------------------------------------------------<br><br>Message: 1<br>Date: Fri, 11 Jun 2010 09:57:34 -0400<br>From: "Ken G." <<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>><br>To: <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: [Tutor] Looking for duplicates within a list<br>Message-ID: <<a ymailto="mailto:4C1240CE.7050807@insightbb.com" href="/mc/compose?to=4C1240CE.7050807@insightbb.com">4C1240CE.7050807@insightbb.com</a>><br>Content-Type: text/plain; charset=ISO-8859-1; format=flowed<br><br>I have been working on this problem for several days and I am not making <br>any progress.
I have a group of 18 number, in ascending order, within a <br>list. They ranged from 1 to 39. Some numbers are duplicated as much as <br>three times or as few as none.<br><br>I started with one list containing the numbers. For example, they are <br>listed as like below:<br><br>a = [1, 2, 3, 3, 4]<br><br>I started off with using a loop:<br><br> for j in range (0, 5):<br> x = a[0] # for example, 1<br><br>How would I compare '1' with 2, 3, 3, 4? <br><br>Do I need another duplicated list such as b = a and compare a[0] with <br>either b[0], b[1], b[2], b[3], b[4]?<br><br>Or do I compare a[0] with a[1], a[2], a[3], a[4]?<br><br>In any event, if a number is listed more than once, I would like to know <br>how many times, such as 2 or 3 times. For example, '3' is listed twice <br>within a list.<br><br>TIA,<br><br>Ken<br><br><br><br><br><br><br><br><br><br><br>------------------------------<br><br>Message:
2<br>Date: Fri, 11 Jun 2010 10:20:20 -0400<br>From: Alex Hall <<a ymailto="mailto:mehgcap@gmail.com" href="/mc/compose?to=mehgcap@gmail.com">mehgcap@gmail.com</a>><br>To: "Ken G." <<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>><br>Cc: <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: Re: [Tutor] Looking for duplicates within a list<br>Message-ID:<br> <<a ymailto="mailto:AANLkTilR4cZMiC1tsAMuTuzO8C32SO-qKDXe9spco681@mail.gmail.com" href="/mc/compose?to=AANLkTilR4cZMiC1tsAMuTuzO8C32SO-qKDXe9spco681@mail.gmail.com">AANLkTilR4cZMiC1tsAMuTuzO8C32SO-qKDXe9spco681@mail.gmail.com</a>><br>Content-Type: text/plain; charset=ISO-8859-1<br><br>On 6/11/10, Ken G. <<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>> wrote:<br>> I have
been working on this problem for several days and I am not making<br>> any progress. I have a group of 18 number, in ascending order, within a<br>> list. They ranged from 1 to 39. Some numbers are duplicated as much as<br>> three times or as few as none.<br>FYI, Python's "set" data type will let you have a list and never have<br>a repeat. I know that is not your goal now, but if you want to remove<br>duplicates, it seems like a good choice.<br>><br>> I started with one list containing the numbers. For example, they are<br>> listed as like below:<br>><br>> a = [1, 2, 3, 3, 4]<br>><br>> I started off with using a loop:<br>><br>> for j in range (0, 5):<br>> x = a[0] # for example, 1<br>><br>> How would I compare '1' with 2, 3, 3, 4?<br>><br>> Do I need another duplicated list such as b = a and compare a[0] with<br>> either b[0],
b[1], b[2], b[3], b[4]?<br>><br>> Or do I compare a[0] with a[1], a[2], a[3], a[4]?<br>A couple points here. First, you will want to make life easier by<br>saying range(0, len(a)) so that the loop will work no matter the size<br>of a.<br>Second, for comparing a list to itself, here is a rather inefficient,<br>though simple, way:<br><br>for i in range(0, len(a)):<br> x=a[i]<br> for j in range(0, len(a)):<br> y=a[j]<br> if(x==y and i!=j): #match since a[i]==a[j] and i and j are not the<br>same index of a<br>><br>> In any event, if a number is listed more than once, I would like to know<br>> how many times, such as 2 or 3 times. For example, '3' is listed twice<br>> within a list.<br>Do not quote me here, but I think sets may be able to tell you that as well.<br>><br>> TIA,<br>><br>> Ken<br>><br>><br>><br>><br>><br>><br>><br>><br>>
_______________________________________________<br>> Tutor maillist - <a ymailto="mailto:Tutor@python.org" href="/mc/compose?to=Tutor@python.org">Tutor@python.org</a><br>> To unsubscribe or change subscription options:<br>> <a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>><br><br><br>-- <br>Have a great day,<br>Alex (msg sent from GMail website)<br><a ymailto="mailto:mehgcap@gmail.com" href="/mc/compose?to=mehgcap@gmail.com">mehgcap@gmail.com</a>; <a href="http://www.facebook.com/mehgcap" target="_blank">http://www.facebook.com/mehgcap</a><br><br><br>------------------------------<br><br>Message: 3<br>Date: Fri, 11 Jun 2010 10:31:58 -0400<br>From: "Ken G." <<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>><br>To: <a ymailto="mailto:vnbang2003@yahoo.com"
href="/mc/compose?to=vnbang2003@yahoo.com">vnbang2003@yahoo.com</a>, <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: Re: [Tutor] Looking for duplicates within a list<br>Message-ID: <<a ymailto="mailto:4C1248DE.6090508@insightbb.com" href="/mc/compose?to=4C1248DE.6090508@insightbb.com">4C1248DE.6090508@insightbb.com</a>><br>Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"<br><br>vijay wrote:<br>> Check out this code<br>> l= [1, 2, 3, 3, 4]<br>> d={}<br>> for item in l:<br>> d.setdefaut(item,0)<br>> d[item] +=1<br>> print d<br>> {1: 1, 2: 1, 3: 2, 4: 1}<br>><br>><br>> with regard's<br>> vijay<br>><br>><br>Thanks. Very interesting concept.<br><br>Ken<br>-------------- next part --------------<br>An HTML attachment was scrubbed...<br>URL: <<a
href="http://mail.python.org/pipermail/tutor/attachments/20100611/12e042f9/attachment-0001.html" target="_blank">http://mail.python.org/pipermail/tutor/attachments/20100611/12e042f9/attachment-0001.html</a>><br><br>------------------------------<br><br>Message: 4<br>Date: Fri, 11 Jun 2010 10:40:58 -0400<br>From: "Ken G." <<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>><br>To: Alex Hall <<a ymailto="mailto:mehgcap@gmail.com" href="/mc/compose?to=mehgcap@gmail.com">mehgcap@gmail.com</a>><br>Cc: <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: Re: [Tutor] Looking for duplicates within a list<br>Message-ID: <<a ymailto="mailto:4C124AFA.7030007@insightbb.com" href="/mc/compose?to=4C124AFA.7030007@insightbb.com">4C124AFA.7030007@insightbb.com</a>><br>Content-Type: text/plain; charset="iso-8859-1";
Format="flowed"<br><br>Alex Hall wrote:<br>> On 6/11/10, Ken G. <<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>> wrote:<br>> <br>>> I have been working on this problem for several days and I am not making<br>>> any progress. I have a group of 18 number, in ascending order, within a<br>>> list. They ranged from 1 to 39. Some numbers are duplicated as much as<br>>> three times or as few as none.<br>>> <br>> FYI, Python's "set" data type will let you have a list and never have<br>> a repeat. I know that is not your goal now, but if you want to remove<br>> duplicates, it seems like a good choice.<br>> <br>>> I started with one list containing the numbers. For example, they are<br>>> listed as like below:<br>>><br>>> a = [1, 2, 3, 3,
4]<br>>><br>>> I started off with using a loop:<br>>><br>>> for j in range (0, 5):<br>>> x = a[0] # for example, 1<br>>><br>>> How would I compare '1' with 2, 3, 3, 4?<br>>><br>>> Do I need another duplicated list such as b = a and compare a[0] with<br>>> either b[0], b[1], b[2], b[3], b[4]?<br>>><br>>> Or do I compare a[0] with a[1], a[2], a[3], a[4]?<br>>> <br>> A couple points here. First, you will want to make life easier by<br>> saying range(0, len(a)) so that the loop will work no matter the size<br>> of a.<br>> Second, for comparing a list to itself, here is a rather inefficient,<br>> though simple, way:<br>><br>> for i in range(0, len(a)):<br>> x=a[i]<br>> for j in range(0, len(a)):<br>> y=a[j]<br>> if(x==y and i!=j):
#match since a[i]==a[j] and i and j are not the<br>> same index of a<br>> <br>>> In any event, if a number is listed more than once, I would like to know<br>>> how many times, such as 2 or 3 times. For example, '3' is listed twice<br>>> within a list.<br>>> <br>> Do not quote me here, but I think sets may be able to tell you that as well.<br>> <br>>> TIA,<br>>><br>>> Ken<br>>> <br>Thank you for your contribution. As seen here, I have much to learn.<br><br>Ken<br><br>-------------- next part --------------<br>An HTML attachment was scrubbed...<br>URL: <<a href="http://mail.python.org/pipermail/tutor/attachments/20100611/eacd401a/attachment-0001.html"
target="_blank">http://mail.python.org/pipermail/tutor/attachments/20100611/eacd401a/attachment-0001.html</a>><br><br>------------------------------<br><br>Message: 5<br>Date: Fri, 11 Jun 2010 16:46:25 +0200<br>From: Sander Sweers <<a ymailto="mailto:sander.sweers@gmail.com" href="/mc/compose?to=sander.sweers@gmail.com">sander.sweers@gmail.com</a>><br>To: "Ken G." <<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>><br>Cc: <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: Re: [Tutor] Looking for duplicates within a list<br>Message-ID:<br> <<a ymailto="mailto:AANLkTikGt6zifb0gOCSOR5vnvxg2MtFdaWJjr5RHBrb4@mail.gmail.com" href="/mc/compose?to=AANLkTikGt6zifb0gOCSOR5vnvxg2MtFdaWJjr5RHBrb4@mail.gmail.com">AANLkTikGt6zifb0gOCSOR5vnvxg2MtFdaWJjr5RHBrb4@mail.gmail.com</a>><br>Content-Type: text/plain;
charset=UTF-8<br><br>On 11 June 2010 15:57, Ken G. <<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>> wrote:<br>> In any event, if a number is listed more than once, I would like to know how<br>> many times, such as 2 or 3 times. ?For example, '3' is listed twice within a<br>> list.<br><br>If you do not have top keep the order of the number this will work.<br><br>>>> a = [1, 2, 3, 3, 4]<br>>>> counted = {}<br>>>> for n in a:<br> if not n in counted:<br> counted[n] = 1<br> else:<br> counted[n] += 1<br><br>>>> counted<br>{1: 1, 2: 1, 3: 2, 4: 1}<br><br>>>> for x, y in counted.items():<br> if y > 1:<br> print "Number %s was found %s times" % (x,
y)<br> else:<br> print "Number %s was found %s time" % (x, y)<br><br>Number 1 was found 1 time<br>Number 2 was found 1 time<br>Number 3 was found 2 times<br>Number 4 was found 1 time<br><br>Greets<br>Sander<br><br><br>------------------------------<br><br>Message: 6<br>Date: Fri, 11 Jun 2010 15:49:22 +0100<br>From: Jose Amoreira <<a ymailto="mailto:ljmamoreira@gmail.com" href="/mc/compose?to=ljmamoreira@gmail.com">ljmamoreira@gmail.com</a>><br>To: <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: Re: [Tutor] Looking for duplicates within a list<br>Message-ID: <<a ymailto="mailto:201006111549.22751.ljmamoreira@gmail.com" href="/mc/compose?to=201006111549.22751.ljmamoreira@gmail.com">201006111549.22751.ljmamoreira@gmail.com</a>><br>Content-Type: Text/Plain; charset="iso-8859-1"<br><br>On Friday, June 11, 2010 02:57:34 pm Ken
G. wrote:<br>> I have been working on this problem for several days and I am not making<br>> any progress. I have a group of 18 number, in ascending order, within a<br>> list. They ranged from 1 to 39. Some numbers are duplicated as much as<br>> three times or as few as none.<br>> <br>> I started with one list containing the numbers. For example, they are<br>> listed as like below:<br>> <br>> a = [1, 2, 3, 3, 4]<br>> <br>> I started off with using a loop:<br>> <br>> for j in range (0, 5):<br>> x = a[0] # for example, 1<br>> <br>> How would I compare '1' with 2, 3, 3, 4?<br>> <br>> Do I need another duplicated list such as b = a and compare a[0] with<br>> either b[0], b[1], b[2], b[3], b[4]?<br>> <br>> Or do I compare a[0] with a[1], a[2], a[3], a[4]?<br>> <br>> In any event, if a number is listed more than once, I
would like to know<br>> how many times, such as 2 or 3 times. For example, '3' is listed twice<br>> within a list.<br>> <br>> TIA,<br>><br><br>I would do it with a dictionary:<br>def reps(lst):<br> dict = {}<br> for item in lst:<br> if item in dict:<br> dict[item] += 1<br> else:<br> dict[item] = 1<br> return dict<br><br>This function returns a dictionary with of the number of times each value in <br>the list is repeated. Even shorter using dict.setdefault:<br><br>def reps(lst):<br> dict={}<br> for item in lst:<br> dict[item] = dict.setdefault(item,0) + 1<br> return dict<br><br>For instance, if
lst=[1,2,2,2,4,4,5], then reps(lst) returns<br>{1: 1, 2: 3, 4: 2, 5: 1}<br><br>Using the fact that the list is ordered, one can design a more efficient <br>solution (go through the list; if this item is equal to the previous, then it <br>is repeated, else, it is a new value). But you list is short enough for this <br>direct approach to work.<br>Hope this helps. Cheers,<br>Jose<br><br><br>------------------------------<br><br>_______________________________________________<br>Tutor maillist - <a ymailto="mailto:Tutor@python.org" href="/mc/compose?to=Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br><br><br>End of Tutor Digest, Vol 76, Issue 31<br>*************************************<br></div></blockquote></td></tr></table><br>