Kent Johnson said unto the world upon 2005-01-27 16:08:
Brian van den Broek wrote:

<SNIP>

Finally, in the first instance, I was aiming for the OP's stated end. To make this more general and reusable, I think I'd do:

<code>
def get_list_dup_dict(a_list, threshold=1):

    items_dict, dup_dict = {}, {}   # Question below

    for i in a_list:
        items_dict[i] = items_dict.get(i, 0) + 1

    for k, v in items_dict.iteritems():
        if v >= threshold:
            dup_dict[k] = v    #Question below

    return dup_dict

def print_list_dup_report(a_list, threshold=1):

    dup_dict = get_list_dup_dict(a_list, threshold)
    for k, v in sorted(dup_dict.iteritems()):
        print '%s occurred %s times' %(k, v)
</code>


My question (from comment in new code):

Since I've split the task into two functions, one to return a duplication dictionary, the other to print a report based on it, I think the distinct dup_dict is needed. (I do want the get_list_dup_dict function to return a dict for possible use in other contexts.)


You could have a function that gets all of the counts, and then filter on threshold when you print.

<SNIP>

Am I overlooking yet another dict technique that would help, here? Any other improvements?

Thanks Kent. Above the last snip would indeed be one such improvement! (How'd I miss that?)


Best,

Brian vdB

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to