zoukankan      html  css  js  c++  java
  • python的sorted

    读入后,要进行组内排序,按groupseq字段排序后,然后统计前后两个项的个数,累加到全局。

    sorted函数使用如下:

    def sortlist(alllist):
        sorted_key1_1=sorted(alllist,key=lambda k:k['groupseq'])
        return sorted_key1_1

    keylist = readline()
    for key in keylist:
           sortlist=(keylist[key])

    全段代码如下

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    #vim set ts=4 expandtab
    import sys

    keylist={}
    def readline():
        global keylist
        filecontent= open('g:\test.txt','r')
        for line in filecontent.xreadlines():
        #for line in sys.stdin:
            line.strip()
            items=line.split(" ")
            count=0
            appid = items[count]
            count= count+1
            app_version = items[count]
            count= count+1
            act_name=items[count]
            count=count+1
            groupkey=items[count]
            count=count+1
            ggg=items[count]
            groupseq=int(ggg)
            count=count+1
            act_dur=items[count]
            act_dur.strip()
            count=count+1
            key1 = " ".join((appid,app_version,groupkey))
            value1={}
            value1['act_name']=act_name
            value1['groupseq']=groupseq
            value1['act_dur']=act_dur
            if not key1 in keylist:
                keylist[key1]=[]
            keylist[key1].append(value1)
            #it=" ".join((appid,app_version,groupkey,act_name,groupseq,act_dur))
            #print it.strip()
        #return keylist

    def sortlist(alllist):
        sorted_key=sorted(alllist,key=lambda k:k['groupseq'])
        #print sorted_key
        #print type(sorted_key)
        return sorted_key

    if __name__ == '__main__':
         countall={}
         duration={}
         readline()
         for keys in keylist.keys():
             sorted_list=sortlist(keylist[keys])
             #print sorted_list[0]
             length=len(sorted_list)
             #for value in sorted_list:
             prev_page=""
             for i in range(length):
                 current_page = sorted_list[i]['act_name']
                 if "unknown" == current_page:
                     prev_page = current_page  
                     #continue
                 else:
                     num=sorted_list[i]['act_dur']
                     count_duration=float(num.strip())
                     allkeys=keys.split(" ")
                     keycount=' '.join((allkeys[0],allkeys[1],prev_page,current_page))
                     if not keycount in countall:
                        countall[keycount]=0
                     countall[keycount]+=1
                     if not keycount in duration:
                        duration[keycount]=0
                     duration[keycount] += count_duration                   
                     if current_page == "exit":
                         break
                     prev_page = current_page
               
         for key_cou in countall:
             output=" ".join((key_cou,str(countall[key_cou]),str(duration[key_cou])))
             #output = " ".join((key_cou,str(countall[key_cou])))    
             print output.strip()

    这里的列表中,元素为字典。用key传函数,参数为x["key],这里的x在运行时,会被赋成列表中每个字典对象

    f = [{'name':'abc','age':20},{'name':'def','age':30},{'name':'ghi','age':25}]
    def age(s):
        return s['age']
    print sorted(f,key = age)#列表按f中字典的age从小到大排序。也就是说,传给s的是每个字典对象,s是一个字典形参,调用s["key"],则按key排序每个子元素。

    也可以:print sorted(f,key = lambda x:x["name"])。#lambda见下面解释使用时,相当于调用key(x)这个函数,其中x被赋值为传入的对象,在这里是每个子对象字典。返回值为x[0],而正是按这个返回值排序。

    结果如下:
    [{'age': 20, 'name': 'abc'}, {'age': 25, 'name': 'ghi'}, {'age': 30, 'name': 'def'}]

     lambda这个匿名函数,使用如下:

    m = lambda x,y,z: (x-y)*z#x是参数,函数名是标示符m
    print m(3,1,2)#使用时,标示符m作为参数名,,x,y,z作为参数传入。
    结果是4

    总结,m为函数名,xyz为形参,表达式为返回值

    而字典排序:

    sl={'b':2,'a':1,'d':4,'c':3}
    print sorted(sl,key=lambda x:x[0])#等同于:print sorted(sl,key=lambda x:x

    ['a', 'b', 'c', 'd'],注意如果是x[1]就报错了。

    python对容器内数据的排序有两种,一种是容器自己的sort函数,一种是内建的sorted函数。

    不同在于sort是在原位重新排列列表,而sorted()是产生一个新的列表:

    --------------------------------sorted---------------------------------------
    >>> help(sorted)
    Help on built-in function sorted in module __builtin__:
    sorted(...)
    sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
    ---------------------------------sort----------------------------------------
    >>> help(list.sort)
    Help on method_descriptor:
    sort(...)
    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    -----------------------------------------------------------------------------

    >>> print sorted([5, 2, 3, 1, 4])
    [1, 2, 3, 4, 5]
    >>> L = [5, 2, 3, 1, 4]
    >>> L.sort()
    >>> print L
    [1, 2, 3, 4, 5]

    def lastchar(s): 
                return s[-1]
    e = ['abc','b','AAz','ef']
    sorted(e,key = lastchar) #自定义函数排序,lastchar为函数名,这个函数返回列表e中每个元素的最后一个字母
    ['b', 'abc', 'ef', 'AAz'] #sorted(e,key=lastchar)作用就是 按列表e中每个元素的最后一个字母的ascii码从小到大排序

    sorted(...)
    sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

    iterable:是可迭代类型;
    cmp:是带两个参数的比较函数,比较内容由另一个参数key决定,返回值: 负数: e1 < e2, 0: e1 == e2, 正数: e1 > e2. 默认为 None, 即内建的比较函数.
    key:是带一个参数的函数, 用来为每个元素提取比较值. 默认为 None, 即直接比较每个元素.
    reverse:排序规则. reverse = True 或者 reverse = False,有默认值。
    返回值:是一个排序的可迭代类型,与iterable一样。

    通常, key 和 reverse 比 cmp 快很多, 因为对每个元素它们只处理一次; 而 cmp 会处理多次

    对由字典排序

    1. >>> d = {'data1':3,'data2':1,'data3':2,'data4':4}
    2. >>> sorted(d.iteritems(), key=itemgetter(1), reverse=True)
    3. [('data4', 4), ('data1',3), ('data3',2), ('data2',1)]

    引自:http://www.cnblogs.com/linyawen/archive/2012/03/15/2398292.html

    Python的内置dictionary数据类型是无序的,通过key来获取对应的value。可是有时我们需要对dictionary中 的item进行排序输出,可能根据key,也可能根据value来排。

    list的排序,使用如下

    使用cmp:

    >>>L = [('b',2),('a',1),('c',3),('d',4)]
    >>>print sorted(L, cmp=lambda x,y:cmp(x[1],y[1]))
    [('a', 1), ('b', 2), ('c', 3), ('d', 4)]

    使用keys:

    >>>L = [('b',2),('a',1),('c',3),('d',4)]
    >>>print sorted(L, key=lambda x:x[1]))
    [('a', 1), ('b', 2), ('c', 3), ('d', 4)]


    reverse是决定正序还是倒序的:

    >>> print sorted([5, 2, 3, 1, 4], reverse=True)
    [5, 4, 3, 2, 1]
    
    >>> print sorted([5, 2, 3, 1, 4], reverse=False)
    [1, 2, 3, 4, 5]
    
    注:效率key>cmp(key比cmp快)
    
    
    在Sorting Keys中:我们看到,此时排序过的L是仅仅按照第二个关键字来排的,如果我们想用第二个关键字
    排过序后再用第一个关键字进行排序呢?
    >>> L = [('d',2),('a',4),('b',3),('c',2)]
    >>> print sorted(L, key=lambda x:(x[1],x[0]))
    >>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]

    用 operator 函数来加快速度, 上面排序等价于:(itemgetter的用法见 注释2)

    Python代码 
    1. >>> from operator import itemgetter, attrgetter
    2. >>> sorted(students, key=itemgetter(2))
    >>> from operator import itemgetter, attrgetter
    >>> sorted(students, key=itemgetter(2))
    



    用 operator 函数进行多级排序

    Python代码 
    1. >>> sorted(students, key=itemgetter(1,2))# sort by grade then by age
    2. [('john', 'A',15), ('dave','B',10), ('jane','B',12)]
    >>> sorted(students, key=itemgetter(1,2))  # sort by grade then by age
    [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
    




     

  • 相关阅读:
    互联网思维(1)
    互联网思维
    WLAN和WIFI的区别
    ping操作
    一篇关于正则表达式的小结
    javascript正则表达式
    为什么原型继承很重要 – SegmentFault
    JS面向对象基础讲解(工厂模式、构造函数模式、原型模式、混合模式、动态原型
    【转】前端开发文档规范
    我的第一篇博文
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6205605.html
Copyright © 2011-2022 走看看