[Tutor] Re: Unique Items in Lists
Brian van den Broek
bvande at po-box.mcgill.ca
Thu Jan 27 09:59:05 CET 2005
Wolfram Kraus said unto the world upon 2005-01-27 03:24:
> Brian van den Broek wrote:
<SNIP>
Thanks Wolfram,
I knew someone would improve what I posted. (It can always be done ;-)
>> for i in a_list:
>> if i in items_dict:
>> items_dict[i] = items_dict[i] + 1
>> else:
>> items_dict[i] = 1
>
> get(key, default) is your friend here:
> for i in a_list:
> items_dict[i] = items_dict.get(i, 0) + 1
>
> get() (and his "brother" setdefault()) are mighty dictionary-methods.
What a brain fart I had! I even got as far as reading then entry in
the Library Ref Mapping Types table for setdefault, 'cause I knew
there was a way to do this, but couldn't recall it. How they expect me
to find it when they hide it on the previous line I'll never know ;-)
/ :-[
>> for key in items_dict.copy(): # Try it without the .copy()
>> if items_dict[key] == 1: # and see what happens.
>> del items_dict[key]
>>
>> dict_keys = items_dict.keys()
>> dict_keys.sort()
>
> >
>
>> for key in dict_keys:
>> print '%s occurred %s times' %(key, items_dict[key])
>
> This whole part can be rewritten (without sorting, but in Py2.4 you can
> use sorted() for this) with a list comprehension (Old Python2.1 style,
> with a newer version the keys() aren't needed):
> for k,v in [(k, items_dict[k]) \
> for k in items_dict.keys() if items_dict[k] > 1]:
> print '%s occurred %s times' %(key, items_dict[key])
I knew sorted() only from the 'What's New in 2.4'. Thanks for
reminding me to look closer.
I get that the for loops in mine are more costly than the list comps
in yours. But, I can't help but feel that the list comp isn't as clear
or readable. Of course, that may say more about me (and the fact that
while I can read list comps, they aren't really in my working
vocabulary yet) than it does about the code. I think I like the way I
wrote it in that it breaks the discrete steps apart. But, for long
lists, that will be a performance bite indeed.
(I went from my first posts being too concerned about speed to not
giving a toss. Perhaps the pendulum needs to swing back a bit.)
Anyway, thanks for showing me (and the OP) these alternatives!
Best,
Brian vdB
More information about the Tutor
mailing list