This is just a follow up from your help Chris. Everything worked
out beautifully. The method you suggested really cut down on the
script time and allowed me to reduce a lot of redundant areas.
Thanks again.<br>
<br>
-Derek<br><br><div><span class="gmail_quote">On 6/9/05, <b class="gmail_sendername">Chris Lambacher</b> <<a href="mailto:lambacck@gmail.com">lambacck@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
You are probably going about solving the problem from the wrong direction:<br><br>Try something like this, overly verbose variable names used on purpose:<br><br>regHours = context.getMainPrint();<br><br>#let a dictionary do the grouping for us
<br>hours_to_libraries_dic = {}<br>for lib in regHours:<br> key = lib.Monday + lib.Tuesday + lib.Wednesday + lib.Thursday +<br>lib.Friday + lib.Saturday + lib.Sunday<br> try:<br> # if the key already exists add to the list of libraries
<br> hours_to_libraries_dic[key].append(lib)<br> except KeyError:<br> # if the key does not exists, create a new library list<br> hours_to_libraries_dic[key] = [lib]<br><br><br>#print out the timetable<br>
for lib_group in hours_to_libraries_dic.values():<br> print " and ".join([lib.libraryName for lib in lib_group])<br> a_lib = lib_group[0]<br> print " Monday", a_lib.Monday<br> print " Tuesday", a_lib.Tuesday
<br> print " Wednesday", a_lib.Wednesday<br> ....(you get the idea)<br> print " Sunday", a_lib.Sunday<br> print<br><br><br><br><br>-Chris<br><br>On 6/9/05, Derek Perriero <<a href="mailto:derek.perriero@gmail.com">
derek.perriero@gmail.com</a>> wrote:<br>> Sorry for not being more clear. I'm using Zope to store the hours of each<br>> library on the campus. The hours of each library will be set on a basis of<br>> Monday - Friday.
i.e. Monday for a specific library, let's say Downtown<br>> Campus Library will stored as an attribute of 8am - 9pm, in Zope, and each<br>> day till Friday will be stored as the hours dictate. I'm generating a<br>> print-out based on these hours and info for the general public. The goal of
<br>> mine is to group all the libraries under one heading if they have the exact<br>> same hours, to cut back on redundancy when a user looks at it.<br>> So when I say: collect = item.Monday + item.Tuesday + item.Wednesday
+<br>> item.Thursday + item.Friday + item.Saturday + item.Sunday, the order is<br>> already this preset configuration. I want 'collect' to be static so it can<br>> compare it against another libraries hours and group it if necessary. The
<br>> libraries that fail to be duplicates of other libraries will be generated as<br>> usual under the grouped libraries. They will have a single heading.<br>><br>> An example can be seen here of what I am trying to achieve:
<br>> <a href="http://www.libraries.wvu.edu/hours/summer.pdf">http://www.libraries.wvu.edu/hours/summer.pdf</a><br>> These are the outputs I failed to mention last time.<br>> What I want:<br>> ['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm - 10pm', '8am -
<br>> 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am - 8pm9am -<br>> 8pm9am - 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am - 5pm9am - 5pm9am -<br>> 5pm9am - 5pm9am - 5pmClosedClosed', '10am - 5pm10am - 5pm10am - 5pm10am -
<br>> 5pm10am - 5pm10am - 5pmClosed']<br>><br>> What I am getting now:<br>><br>> ['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm - 10pm', '8am -<br>> 9pm8am - 9pm8am - 9pmClosed8am - 5pm9am - 5pm6pm - 10pm', '8am - 9pm8am -
<br>> 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am - 8pm9am - 8pm9am -<br>> 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am - 5pm9am - 5pm9am - 5pm9am -<br>> 5pm9am - 5pmClosedClosed', '10am - 5pm10am - 5pm10am - 5pm10am - 5pm10am -
<br>> 5pm10am - 5pmClosed']<br>><br>> Thanks,<br>> -Derek<br>><br>><br>> On 6/9/05, Chris Lambacher <<a href="mailto:lambacck@gmail.com">lambacck@gmail.com</a>> wrote:<br>> > It is very unclear what you are trying to do. Why not explain what
<br>> > you want the output to be. You will get better answers.<br>> ><br>> > As a first stab at what you are doing wrong:<br>> > collect = item.Monday + item.Tuesday + item.Wednesday + item.Thursday
<br>> > + item.Friday + item.Saturday + item.Sunday<br>> ><br>> > The above is string addition and the result is a string. The ouput<br>> > you provide is in fact a list with no duplicates, i.e. there are no
<br>> > two strings the same.<br>> ><br>> > If order is not important to you a structure that will give you an<br>> > 'unordered list with no duplicates' is a set (available in the std<br>> > library in Python
2.3 and 2.4, see the cookbook for recipies for<br>> > earlier versions of Python). Note that sets are unordered, i.e. no<br>> > guarentee is made about what order the elements are accessed in when<br>> > you iterate over them.
<br>> ><br>> > -Chris<br>> ><br>> > On 6/9/05, Derek Perriero < <a href="mailto:derek.perriero@gmail.com">derek.perriero@gmail.com</a>> wrote:<br>> > > I've been un-triumphantly trying to get a list of mine to have no
<br>> repeats in<br>> > > it. First, I'm pulling attributes from Zope and forming a list. Next,<br>> I'm<br>> > > pulling those same values and comparing them against the same list and<br>> if
<br>> > > the values equal each other and are not already in the list, they append<br>> to<br>> > > my 'nodupes' list. My current result is showing what I put in I am<br>> getting<br>> > > out. Below the code is my output of 'nodupes' list. Here's the
<br>> snippet.<br>> > ><br>> > > regHours = context.getMainPrint(); <---attributes from Zope<br>> > ><br>> > > libslist = []<br>> > > nodupes = []<br>> > >
<br>> > > #collect libraries<br>> > > for libs in regHours:<br>> > > cache = libs.Monday + libs.Tuesday + libs.Wednesday + libs.Thursday +<br>> > > libs.Friday + libs.Saturday + libs.Sunday
<br>> > > libslist.append (cache)<br>> > ><br>> > > #pull repeated values<br>> > > for item in regHours:<br>> > > collect = item.Monday + item.Tuesday + item.Wednesday +
item.Thursday<br>> +<br>> > > item.Friday + item.Saturday + item.Sunday<br>> > > libName = item.libraryName<br>> > ><br>> > > for libs in libslist:<br>> > > if collect == libs and libs not in nodupes:
<br>> > > nodupes.append(libs)<br>> > ><br>> > > My Current Output:<br>> > > ['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm - 10pm',<br>> '8am -<br>> > > 9pm8am - 9pm8am - 9pmClosed8am - 5pm9am - 5pm6pm - 10pm', '8am - 9pm8am
<br>> -<br>> > > 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am - 8pm9am - 8pm9am -<br>> > > 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am - 5pm9am - 5pm9am -<br>> 5pm9am -<br>> > > 5pm9am - 5pmClosedClosed', '10am - 5pm10am - 5pm10am - 5pm10am - 5pm10am
<br>> -<br>> > > 5pm10am - 5pmClosed']<br>> > ><br>> > > Thanks<br>> > ><br>> > ><br>> > ><br>> > ><br>> > ><br>> > > --<br>> > > Perriero, Derek
<br>> > > <a href="mailto:derek.perriero@gmail.com">derek.perriero@gmail.com</a><br>> > ><br>> > > --<br>> > > <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list
</a><br>> > ><br>> > ><br>> ><br>> ><br>> > --<br>> > Christopher Lambacher<br>> > <a href="mailto:lambacck@computer.org">lambacck@computer.org</a><br>> ><br>><br>>
<br>><br>> --<br>> Perriero, Derek<br>> <a href="mailto:derek.perriero@gmail.com">derek.perriero@gmail.com</a><br>><br>> --<br>> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list
</a><br>><br>><br><br><br>--<br>Christopher Lambacher<br><a href="mailto:lambacck@computer.org">lambacck@computer.org</a><br></blockquote></div><br><br><br>-- <br>Perriero, Derek<br><a href="mailto:derek.perriero@gmail.com">
derek.perriero@gmail.com</a><br>