Re: using def in pythons

2008-12-30 Thread Sreenivas
On Dec 30, 3:37 pm, [email protected] wrote:
> hi,
>
> i want to have a broad knowledge on the use of "def" in python as i
> know i might need it
> to my string handling and for a lot of things in general.
>
> I will really appreciate anybody who can support my mission of
> becoming a python Programmer
> as per constant chatting and support or manuals to read.
>
> thanks

go through this link
http://www.python.org/doc/2.5.1/ref/function.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: tallying occurrences in list

2010-06-05 Thread Sreenivas Reddy Thatiparthy
On Jun 4, 11:14 am, kj  wrote:
> Task: given a list, produce a tally of all the distinct items in
> the list (for some suitable notion of "distinct").
>
> Example: if the list is ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b',
> 'c', 'a'], then the desired tally would look something like this:
>
> [('a', 4), ('b', 3), ('c', 3)]
>
> I find myself needing this simple operation so often that I wonder:
>
> 1. is there a standard name for it?
> 2. is there already a function to do it somewhere in the Python
>    standard library?
>
> Granted, as long as the list consists only of items that can be
> used as dictionary keys (and Python's equality test for hashkeys
> agrees with the desired notion of "distinctness" for the tallying),
> then the following does the job passably well:
>
> def tally(c):
>     t = dict()
>     for x in c:
>         t[x] = t.get(x, 0) + 1
>     return sorted(t.items(), key=lambda x: (-x[1], x[0]))
>
> But, of course, if a standard library solution exists it would be
> preferable.  Otherwise I either cut-and-paste the above every time
> I need it, or I create a module just for it.  (I don't like either
> of these, though I suppose that the latter is much better than the
> former.)
>
> So anyway, I thought I'd ask. :)
>
> ~K

How about this one liner, if you prefer them;
set([(k,yourList.count(k)) for k in yourList])
-- 
http://mail.python.org/mailman/listinfo/python-list