Remove duplicates from list

Chris Lambacher lambacck at gmail.com
Thu Jun 9 23:33:08 CEST 2005


You are probably going about solving the problem from the wrong direction:

Try something like this, overly verbose variable names used on purpose:

regHours  = context.getMainPrint();

#let a dictionary do the grouping for us
hours_to_libraries_dic = {}
for lib in regHours:
   key = lib.Monday + lib.Tuesday + lib.Wednesday + lib.Thursday +
lib.Friday + lib.Saturday + lib.Sunday
   try:
      # if the key already exists add to the list of libraries
      hours_to_libraries_dic[key].append(lib)
   except KeyError:
      # if the key does not exists, create a new library list
      hours_to_libraries_dic[key] = [lib]


#print out the timetable
for lib_group in hours_to_libraries_dic.values():
    print " and ".join([lib.libraryName for lib in lib_group])
    a_lib = lib_group[0]
    print "    Monday", a_lib.Monday
    print "    Tuesday", a_lib.Tuesday
    print "    Wednesday", a_lib.Wednesday
    ....(you get the idea)
    print "    Sunday", a_lib.Sunday
    print




-Chris

On 6/9/05, Derek Perriero <derek.perriero at gmail.com> wrote:
> Sorry for not being more clear.  I'm using Zope to store the hours of each
> library on the campus.  The hours of each library will be set on a basis of
> Monday - Friday. i.e. Monday for a specific library, let's say Downtown
> Campus Library will stored as an attribute of 8am - 9pm, in Zope, and each
> day till Friday will be stored as the hours dictate.  I'm generating a
> print-out based on these hours and info for the general public.  The goal of
> mine is to group all the libraries under one heading if they have the exact
> same hours, to cut back on redundancy when a user looks at it.  
>  So when I say: collect = item.Monday + item.Tuesday + item.Wednesday +
> item.Thursday + item.Friday + item.Saturday + item.Sunday,  the order is
> already this preset configuration.  I want 'collect' to be static so it can
> compare it against another libraries hours and group it if necessary.  The
> libraries that fail to be duplicates of other libraries will be generated as
> usual under the grouped libraries.  They will have a single heading.
>  
>  An example can be seen here of what I am trying to achieve:
> http://www.libraries.wvu.edu/hours/summer.pdf
>  These are the outputs I failed to mention last time.
>  What I want:
>  ['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm - 10pm', '8am -
> 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am - 8pm9am -
> 8pm9am - 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am - 5pm9am - 5pm9am -
> 5pm9am - 5pm9am - 5pmClosedClosed', '10am - 5pm10am - 5pm10am - 5pm10am -
> 5pm10am - 5pm10am - 5pmClosed']
>  
>  What I am getting now:
>  
>  ['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm - 10pm', '8am -
> 9pm8am - 9pm8am - 9pmClosed8am - 5pm9am - 5pm6pm - 10pm', '8am - 9pm8am -
> 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am - 8pm9am - 8pm9am -
> 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am - 5pm9am - 5pm9am - 5pm9am -
> 5pm9am - 5pmClosedClosed', '10am - 5pm10am - 5pm10am - 5pm10am - 5pm10am -
> 5pm10am - 5pmClosed']
>  
>  Thanks,
>  -Derek
>  
> 
> On 6/9/05, Chris Lambacher <lambacck at gmail.com> wrote:
> > It is very unclear what you are trying to do.  Why not explain what
> > you want the output to be.  You will get better answers.
> > 
> > As a first stab at what you are doing wrong:
> > collect = item.Monday + item.Tuesday + item.Wednesday + item.Thursday
> > + item.Friday + item.Saturday + item.Sunday
> > 
> > The above is string addition and the result is a string.  The ouput
> > you provide is in fact a list with no duplicates, i.e. there are no 
> > two strings the same.
> > 
> > If order is not important to you a structure that will give you an
> > 'unordered list with no duplicates' is a set (available in the std
> > library in Python 2.3 and 2.4, see the cookbook for recipies for 
> > earlier versions of Python).  Note that sets are unordered, i.e. no
> > guarentee is made about what order the elements are accessed in when
> > you iterate over them.
> > 
> > -Chris
> > 
> > On 6/9/05, Derek Perriero < derek.perriero at gmail.com> wrote:
> > > I've been un-triumphantly trying to get a list of mine to have no
> repeats in
> > > it.   First, I'm pulling attributes from Zope and forming a list.  Next,
> I'm 
> > >  pulling those same values and comparing them against the same list and
> if
> > > the values equal each other and are not already in the list, they append
> to
> > > my 'nodupes' list.  My current result is showing what I put in I am
> getting 
> > > out.  Below the code is my output of 'nodupes' list.  Here's the
> snippet.
> > >
> > >  regHours  = context.getMainPrint();  <---attributes from Zope
> > >
> > >  libslist       = []
> > >  nodupes    = [] 
> > >
> > >  #collect libraries
> > >  for libs in regHours:
> > >    cache = libs.Monday + libs.Tuesday + libs.Wednesday + libs.Thursday +
> > > libs.Friday + libs.Saturday + libs.Sunday
> > >    libslist.append (cache)
> > >
> > >  #pull repeated values
> > >  for item in regHours:
> > >    collect = item.Monday + item.Tuesday + item.Wednesday + item.Thursday
> +
> > > item.Friday + item.Saturday + item.Sunday
> > >    libName = item.libraryName
> > >
> > >    for libs in libslist:
> > >      if collect == libs and libs not in nodupes:
> > >        nodupes.append(libs)
> > >
> > >  My Current Output:
> > >  ['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm - 10pm',
> '8am - 
> > > 9pm8am - 9pm8am - 9pmClosed8am - 5pm9am - 5pm6pm - 10pm', '8am - 9pm8am
> -
> > > 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am - 8pm9am - 8pm9am -
> > > 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am - 5pm9am - 5pm9am -
> 5pm9am - 
> > > 5pm9am - 5pmClosedClosed', '10am - 5pm10am - 5pm10am - 5pm10am - 5pm10am
> -
> > > 5pm10am - 5pmClosed']
> > >
> > >  Thanks
> > >
> > >
> > >
> > >
> > >
> > > --
> > > Perriero, Derek
> > > derek.perriero at gmail.com
> > >
> > > --
> > > http://mail.python.org/mailman/listinfo/python-list
> > > 
> > >
> > 
> > 
> > --
> > Christopher Lambacher
> > lambacck at computer.org
> > 
> 
> 
> 
> -- 
> Perriero, Derek
> derek.perriero at gmail.com 
>  
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
> 


-- 
Christopher Lambacher
lambacck at computer.org



More information about the Python-list mailing list