python 字典 get 小例子
语法
get()方法语法:
dict.get(key, default=None)
参数
- key -- 字典中要查找的键。
- default -- 如果指定键的值不存在时,返回该默认值值。
返回值
返回指定键的值,如果值不在字典中返回默认值None。
实例
以下实例展示了 get()函数的使用方法:
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 27}
print "Value : %s" % dict.get('Age')
print "Value : %s" % dict.get('Sex', "Never")
以上实例输出结果为:
Value : 27
Value : Never
access log例子
#!/usr/bin/pyhton
f = open('access.log')
res = {}
print f
for line in f:
#print line
ip = line.split(' ')[0]
#print ip
#print ip.split(' ')
res[ip] = res.get(ip,0)+1 print res
f.close()
排序对log
#!/usr/bin/pyhton
f = open('access.log')
res = {}
#print f
for line in f:
#print line
ip = line.split(' ')[0]
#print ip
#print ip.split(' ')
res[ip] = res.get(ip,0)+1
f.close()
#print res.items()i
res_list = res.items()
for j in range(10):
for i in range(len(res_list)-1):
if res_list[i][1] > res_list[i+1][1]:
res_list[i],res_list[i+1] = res_list[i+1],res_list[i]
i = 0
html_str = '<table border="1px">'
for r in res_list[:-10:-1]:
i = i+1
html_str += '<tr><td>No%s </td> <td>%s</td><td> %s </td></tr>' %(i,r[0],r[1])
html_str +='</table>' html_f = open('res.html','w')
html_f.write(html_str)
#print res
html_f.close()结果
No1 192.168.50.104 6558 No2 192.168.50.106 3700 No3 192.168.50.19 123 No4 192.168.50.105 74 No5 192.168.50.109 13 No6 192.168.50.62 9 No7 192.168.50.183 6 No8 192.168.50.54 4 No9 192.168.50.56 4
根据两个指标来排序
#!/usr/bin/pyhton
f = open('access.log')
res = {}
#print f
for line in f:
#print line
tmp = line.split(' ')
ip,url = tmp[0],tmp[6]
#print ip
#print ip.split(' ')
res[(ip,url)] = res.get((ip,url),0)+1
for key in res:
print key,'----',res[key]
f.close()
python 字典 get 小例子的更多相关文章
- Python,while循环小例子--猜拳游戏(三局二胜)
Python,while循环小例子--猜拳游戏(三局二胜) import random all_choice = ['石头', '剪刀', '布'] prompt = '''(0)石头 (1)剪刀 ( ...
- python字典循环小点
对于字典数据量不大时候 首先会将字典转换成列表,写到内存,如果几百万条,就会减慢速度 for k,v in dic:print k,v 可以这样 for k in dic:print k,dic[ ...
- python,栈的小例子
''' 1.首先确认栈的概念,先进后出 2.初始化的时候如果给了一个数组那么就要将数组进栈 ''' class Stack: def __init__(self,start=[]): self.sta ...
- [Spark][Python]Spark Join 小例子
[training@localhost ~]$ hdfs dfs -cat people.json {"name":"Alice","pcode&qu ...
- python事件驱动的小例子
首先我们写一个超级简单的web框架 event_list = [] #这个event_list中会存放所有要执行的类 def run(): for event in event_list: obj = ...
- 由Python的一个小例子想到的
习题: L = [1,2] L.append(L) Print L 问,结果是什么. 结果是,[1,2,[...]] 这是什么意思呢?就是说[...]表示的对[1,2]的无限循环.这一点是在C#等静态 ...
- python 基础 列表 小例子
存主机ip到列表 host_list=[] netip='192.168.1' for hostip in range(1,254): ip = netip +str(hostip) host_lis ...
- Python,for循环小例子--99乘法表
一.99乘法表 for i in range(1, 10): for j in range(1, i + 1): print('%sx%s=%s ' % (j, i, j * i), end='') ...
- 这42个Python小例子,太走心
告别枯燥,60秒学会一个Python小例子.奔着此出发点,我在过去1个月,将平时经常使用的代码段换为小例子,分享出来后受到大家的喜欢. 一.基本操作 1 链式比较 i = 3print(1 < ...
随机推荐
- 树莓派+pythonista实时监控系统
客户端(pythonista) import ui from PIL import Image import socket, time, StringIO global closeFlat close ...
- 云计算服务的三种类型(SaaS、PaaS、IaaS)
云计算可以帮助企业降低IT方面的成本和复杂性,并获得他们蓬勃发展所需的灵活性与敏捷性.但是,规划出通往云的明确路径并非易事.毕竟用户需要看透与云相关的市场大肆宣传,然后理解并分析不同种类的云计算模式的 ...
- LeetCode:颜色分类【75】
LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...
- 每天一个Linux命令(4)touch命令
touch命令有两个功能:一是用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来:二是用来创建新的空文件. (1)用法 用法:touch [选项]... ...
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-tha ...
- Please enable network time synchronisation in system settings
eth区块同步出现这样的WARN: WARN [06-17|13:02:42] System clock seems off by -51.509894715s, which can prevent ...
- 同一台电脑关于多个SSH KEY管理
运用shh -T -v git@github.com查看具体出错信息,再根据信息来调试 原文链接: http://yijiebuyi.com/blog/f18d38eb7cfee860c117d ...
- Struts2的工作流程分析
Struts2的工作流程分析 Posted on 2011-02-22 09:32 概述 本章讲述Struts2的工作原理. 读者如果曾经学习过Struts1.x或者有过Struts1.x的开发经验, ...
- Blog迁移至Jekyll
后续的Blog都将在 http://zhwbqd.github.io/ 为您呈现
- cpu架构
转自 http://blog.csdn.net/wyzxg/article/details/5027738 CPU架构 Architecture ,结构.架构,这个词用于 CPU 的时候是指 CPU ...