[Tutor] Looking for duplicates within a list
mehgcap at gmail.com
Fri Jun 11 16:20:20 CEST 2010
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 # 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 with
> either b, b, b, b, b?
> Or do I compare a with a, a, a, a?
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
Second, for comparing a list to itself, here is a rather inefficient,
though simple, way:
for i in range(0, len(a)):
for j in range(0, len(a)):
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.
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap
More information about the Tutor