美文网首页
Python乱函

Python乱函

作者: kEvin1986 | 来源:发表于2015-03-04 22:48 被阅读0次

记录一些写过的小函数

-------

Counter

这是特么一段用在Python2.6上的Counter. 叫你们不升级Python TAT!!...

def Counter(litems,sort=False,reverse=False,top=0):
    rdict = {}
    map(lambda x:rdict.update({x,rdict.get(x,0)+1}), litems)
    if sort:
        if top>0:
            return sorted(rdict.items(),key=sort,reverse=reverse)[:top]
        else:
            return sorted(rdict.items(),key=sort,reverse=reverse)
    else:
        return rdict.itms()

相关文章

网友评论

      本文标题:Python乱函

      本文链接:https://www.haomeiwen.com/subject/nybmxttx.html