对数据处理的一些简单方法

1.使得算法的复杂度降到最低。

2.算法尽量简洁,不用写重复的语句。比如for循环一个东西很多次。

3.比如有如下结构,怎么取出出现次数最多的9个手机号

            [
                {
                    "start_time":"2018-12-21 19:40:00",
                    "phone":"075581203960",
                    "location":"null",
                    "phone_location":"广东-深圳",
                    "contact_name":"",
                    "type":0,
                    "calling_duration":63
                },
                {
                    "start_time":"2018-12-21 10:56:22",
                    "phone":"13846018523",
                    "location":"null",
                    "phone_location":"黑龙江-鸡西市",
                    "contact_name":"",
                    "type":0,
                    "calling_duration":22
                },
                {
                    "start_time":"2018-12-21 09:23:03",
                    "phone":"15027723165",
                    "location":"null",
                    "phone_location":"河北-石家庄市",
                    "contact_name":"",
                    "type":0,
                    "calling_duration":623
                },
]

使用下面的方法进行比较,取值

    def compare(self,a, b):
        if a[1] > b[1]:
            return -1
        elif a[1] == b[1]:
            return 0
        else:
            return 1

    def max_list(self,origin_list):
        # 返回列表中重复次数最多的9个手机号组成列表
        phone_dic = {}
        for d in origin_list:
            if len(d['phone']) == 11:
                phone_dic[d['phone']] = phone_dic.get(d['phone'], 0) + 1
        max_list = map(lambda x: x[0], sorted(phone_dic.items(), cmp=self.compare)[:9])
        return max_list

然后传入如上的列表内套的字典的格式,我们就可以取出联系人最频繁的9个手机号。

 

posted @ 2019-01-09 15:07  G先生  阅读(13)  评论(0)    收藏  举报

:guocheng