Smart text parsing

Josiah Carlson jcarlson at nospam.uci.edu
Thu Feb 5 22:57:27 EST 2004


> s = "Hello this is my 1 million word text".split()
> 
> s2 = s.split()
> dict = {}
> for i in s2:         # the loop needs 10s
>         if dict.has_key(i):
>                 dict[i] += 1
>         else:
>                 dict[i] = 1

> list = dict.items()
 > #   this is slow:
 > list.sort(lambda x,y: 2*(x[1] < y[1])-1)


list = zip(dict.values(), dict.keys())
list.sort()

Should be faster due to not using the sort function argument.

  - Josiah



More information about the Python-list mailing list