Problem regarding returning list

Ian Kelly ian.g.kelly at gmail.com
Sun Apr 3 04:02:28 EDT 2011


On Sun, Apr 3, 2011 at 1:12 AM, sl33k <ahsanbagwan at gmail.com> wrote:
> I am trying to return a list of items modified with each item also
> showing like the number of modifications.
>
> Returning a list of user modified items was done easily but I would
> also like to display the item modified by the user and the
> modifications of that particular item.
> Typical way it coould be displayed like is,
> Modified item: No of modifications to it
>
> E.g. sl33k: 3
>
> Some background of the base class methods used:
>
> list_revisions() - gets the list of ints of the all modification of a
> particular item
> get_revision() - given the modification int, it gets the specific
> modified item.
> item.name gives the name of the item
> The method takes for argument the list of items.
>
> I start by declaring a set() of the total modified items. Using for
> loop in the items and in the for loop for particular `int`
> modification of it, I collect the modified item.
> But, I get stuck around when I have to collect the no of modifications
> for one. The set is returned as a list to a template engine for the
> display.
>
> So, I would like to collect for each item, its no of modifications.
> How would I go about doing this? How would i return it with list of
> modified items tp display the above shown example?
>
> Any pointers will be much appreciated.

Instead of a set, you should use a dict, where the keys are the items
and the values are the numbers of modifications.  Your return value
would either be the dict itself or the result of dict.items().

If you're using a recent enough version of Python, you might also have
a look at the collections.Counter class, which is a dict subclass that
is specialized for counting things.

Cheers,
Ian



More information about the Python-list mailing list