Python对json排序分组
res_dl是一个json字符串,如下:
[{'time': '2020/8/10 12:37:29', 'url': 'http://www.baidu.com/Account/QrCodeLoginPost?returnUrl=127.0.0.1', 'ip': '120.229.137.87'}, {'time': '2020/8/10 12:37:25', 'url': 'http://www.baidu.com/Account/QrCodeLoginPost?returnUrl=www.baidu.com', 'ip': '120.229.137.87'}, {'time': '2020/8/10 12:37:20', 'url': 'http://www.baidu.com/Account/QrCodeLoginPost?returnUrl=/www.baidu.com', 'ip': '120.229.137.87'}, {'time': '2020/8/10 12:37:16', 'url': 'http://www.baidu.com/Account/QrCodeLoginPost?returnUrl=http://www.baidu.com', 'ip': '120.229.137.87'}, {'time': '2020/8/10 12:37:07', 'url': 'http://www.baidu.com/Account/QrCodeLoginPost?returnUrl=', 'ip': '120.229.137.87'}, {'time': '2020/8/10 12:37:09', 'url': 'http://www.baidu.com/Account/QrCodeLoginPost?returnUrl=1', 'ip': '120.229.137.87'}, {'time': '2020/8/10 12:36:47', 'url': 'http://www.baidu.com/Account/LoginPost', 'ip': '120.229.137.87'}, {'time': '2020/8/10 12:36:41', 'url': 'http://www.baidu.com/Account/LoginPost', 'ip': '120.229.137.87'}, {'time': '2020/8/9 3:14:16', 'url': 'http://www.baidu.com/Account/QrCodeLoginPost?returnUrl=https://qy.xin.com/', 'ip': '40.77.190.118'}, {'time': '2020/8/9 2:22:04', 'url': 'http://www.baidu.com/Gemfile', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:04', 'url': 'http://www.baidu.com/Gemfile', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:02', 'url': 'http://www.baidu.com/images/images - Copy', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:02', 'url': 'http://www.baidu.com/images/mt', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:02', 'url': 'http://www.baidu.com/images/images (copy)', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:03', 'url': 'http://www.baidu.com/Gemfile', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:02', 'url': 'http://www.baidu.com/images/de', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:02', 'url': 'http://www.baidu.com/images/fr', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:02', 'url': 'http://www.baidu.com/images/en', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:02', 'url': 'http://www.baidu.com/images/tcpdf', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:01', 'url': 'http://www.baidu.com/images/fileserver', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:01', 'url': 'http://www.baidu.com/images/wp', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:01', 'url': 'http://www.baidu.com/images/help', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:01', 'url': 'http://www.baidu.com/images/index_files', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:01', 'url': 'http://www.baidu.com/images/reset', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:00', 'url': 'http://www.baidu.com/images/utf8', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:01', 'url': 'http://www.baidu.com/images/forgot', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:00', 'url': 'http://www.baidu.com/images/lostpassword', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:00', 'url': 'http://www.baidu.com/images/cerberusweb', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:00', 'url': 'http://www.baidu.com/images/uri', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:59', 'url': 'http://www.baidu.com/images/usercp', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:22:00', 'url': 'http://www.baidu.com/images/url', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:59', 'url': 'http://www.baidu.com/images/manual', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:59', 'url': 'http://www.baidu.com/images/manuals', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:59', 'url': 'http://www.baidu.com/images/latest', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:58', 'url': 'http://www.baidu.com/images/release', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:58', 'url': 'http://www.baidu.com/images/access', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:57', 'url': 'http://www.baidu.com/images/page', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:58', 'url': 'http://www.baidu.com/images/doc', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:57', 'url': 'http://www.baidu.com/images/misc', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:57', 'url': 'http://www.baidu.com/images/servlet', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:57', 'url': 'http://www.baidu.com/images/storage', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:56', 'url': 'http://www.baidu.com/images/folder', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:56', 'url': 'http://www.baidu.com/images/space', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:56', 'url': 'http://www.baidu.com/images/static', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:55', 'url': 'http://www.baidu.com/images/site', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:56', 'url': 'http://www.baidu.com/images/version', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:55', 'url': 'http://www.baidu.com/images/projects', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:54', 'url': 'http://www.baidu.com/images/PHP', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:54', 'url': 'http://www.baidu.com/images/index', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:55', 'url': 'http://www.baidu.com/images/contents', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:55', 'url': 'http://www.baidu.com/images/music', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:55', 'url': 'http://www.baidu.com/images/Index', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:53', 'url': 'http://www.baidu.com/images/browse', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:54', 'url': 'http://www.baidu.com/images/demo', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:53', 'url': 'http://www.baidu.com/images/view', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:54', 'url': 'http://www.baidu.com/images/php', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:54', 'url': 'http://www.baidu.com/images/thread', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:52', 'url': 'http://www.baidu.com/images/forms', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:53', 'url': 'http://www.baidu.com/images/user', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:53', 'url': 'http://www.baidu.com/images/portal', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:52', 'url': 'http://www.baidu.com/images/flash', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:53', 'url': 'http://www.baidu.com/images/xml', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:52', 'url': 'http://www.baidu.com/images/templates', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:51', 'url': 'http://www.baidu.com/images/content', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:52', 'url': 'http://www.baidu.com/images/media', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:52', 'url': 'http://www.baidu.com/images/main', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:51', 'url': 'http://www.baidu.com/images/security', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:51', 'url': 'http://www.baidu.com/images/2014', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:50', 'url': 'http://www.baidu.com/images/10', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:51', 'url': 'http://www.baidu.com/images/2013', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:51', 'url': 'http://www.baidu.com/images/2012', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:50', 'url': 'http://www.baidu.com/images/2011', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:50', 'url': 'http://www.baidu.com/images/6', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:50', 'url': 'http://www.baidu.com/images/8', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:49', 'url': 'http://www.baidu.com/images/5', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:50', 'url': 'http://www.baidu.com/images/9', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:50', 'url': 'http://www.baidu.com/images/7', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:49', 'url': 'http://www.baidu.com/images/00', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:49', 'url': 'http://www.baidu.com/images/3', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:49', 'url': 'http://www.baidu.com/images/2', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:49', 'url': 'http://www.baidu.com/images/4', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:48', 'url': 'http://www.baidu.com/images/0', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:49', 'url': 'http://www.baidu.com/images/1', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:48', 'url': 'http://www.baidu.com/images/w', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:48', 'url': 'http://www.baidu.com/images/q', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:48', 'url': 'http://www.baidu.com/images/v', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:48', 'url': 'http://www.baidu.com/images/t', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:48', 'url': 'http://www.baidu.com/images/z', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:48', 'url': 'http://www.baidu.com/images/s', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:47', 'url': 'http://www.baidu.com/images/o', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:48', 'url': 'http://www.baidu.com/images/p', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:48', 'url': 'http://www.baidu.com/images/r', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:47', 'url': 'http://www.baidu.com/images/n', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:47', 'url': 'http://www.baidu.com/images/j', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:47', 'url': 'http://www.baidu.com/images/l', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:47', 'url': 'http://www.baidu.com/images/m', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:47', 'url': 'http://www.baidu.com/images/k', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:47', 'url': 'http://www.baidu.com/images/i', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:47', 'url': 'http://www.baidu.com/images/h', 'ip': '120.229.137.87'}, {'time': '2020/8/9 2:21:47', 'url': 'http://www.baidu.com/images/f', 'ip': '120.229.137.87'}]
以ip为关键字进行分组排序:
#先对返回结果按IP排序,然后再使用groupby分组计算ip访问数量 sorted_res_dl = sorted(res_dl, key = lambda x:x["ip"]) ret = list() for _, group in groupby(sorted_res_dl, lambda x: x.get('ip')): ret.append(list(group)) print(ret) #分组完成 for item in ret: print('%s,扫描 %d 次'% (item[0]['ip'],len(item)))