haixin3036 发表于 2018-8-16 09:02:04

从零开始学Python-day5

  Python--Day5
  学习要有定位,明确目标地去学习。希望自己能坚持下去,并有所收获---leaves
  python04 -- python基础知识总结以及函数进阶
  一、python中获取帮助
  python中获取帮助的方法有两种:
  1.dir("数据类型")===>看所有方法
  2.help("数据类型或者方法") ===>查看官方文档
  ##代码举例:
  ##dir()
  >>> dir([])
  ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__',
  '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
  '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__',
  '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
  '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__',
  '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count',
  'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
  ##help()
  help([].append)
  Help on built-in function append:
  append(...)
  L.append(object) -- append object to end
  二、字典的高阶用法
  常用字典的两种高阶用法dict.get()和dict.setdefault()
  1.dict.get()    ==>主要作用是获取key对应的value值,若字典中无该key值则以给定的value赋予该key.且字典新增一对key,value值。
  2.dict.setdefault() ===>主要针对不存在的key设置默认值
  ##代码举例如下:
  ##dict.get()用法
  In : d = {"192.168":1}
  In : str = "192.168.1"
  In : d = d.get(str,0)+1
  In : d
  Out: {'192.168': 1, '192.168.1': 1}
  In : str = "192.168"
  In : d = d.get(str,0)+1
  In : d
  Out: {'192.168': 2, '192.168.1': 1}
  ##dict.setdefault()
  for k ,v in res.items():
  new.setdefault(v,[])
  new.append(k)
  三、list和dict的区别及相互之间的转换
  3.1 list和dict的区别
  相同点:都可以存储各种数据结构,可以嵌套
  不同点:
  list 有序
  查找需要遍历,速度慢
  dict 没顺序
  查找非常迅速
  3.2 list和dict的转换
  dict--->list    用items函数
  list --->dictenumerate() 或者使用dict.fromkeys()函数
  ##具体代码如下:
  ##dict--->list
  In : d.items()
  Out: [('192.168', 2), ('192.168.1', 1)]
  ##list --->dict
  ##enumerate()
  In : l = list("hello")
  In : print dict((k,v) for k ,v in enumerate(l))
  {0: 'h', 1: 'e', 2: 'l', 3: 'l', 4: 'o'}
  ##fromkeys()的用法
  In : d.fromkeys()
  Out: {1: None, 2: None, 3: None}
  In : d.fromkeys(['name','age'])
  Out: {'age': None, 'name': None}
  In : d
  Out: {'age': 12, 'name': 'test'}
  In : d.fromkeys(['one','two'],['1','2'])
  Out: {'one': ['1', '2'], 'two': ['1', '2']}
  In : d
  Out: {'age': 12, 'name': 'test'}
  In : d.fromkeys(d,['1','2'])
  Out: {'age': ['1', '2'], 'name': ['1', '2']}
  In : d
  Out: {'age': 12, 'name': 'test'}
  In : {}.fromkeys(d,)
  Out: {'age': , 'name': }
  四、文件的处理
  4.1 python中文件的打开方式
  方法一:
  f = open("a.txt")
  f.read()
  f.close()##这个模式操作文件要记得关闭文件,即一定有f.close()
  方法二:
  with open('a.txt') as f:
  f.read()
  ###注意该方法提供了自动关闭文件的功能
  ###with可以同时打开多个文件
  ##使用with打开多个文件:
  #方法一:
  with open('file1') as f1:
  with open('file2') as f2:
  with open('file3') as f3:
  for i in f1:
  j = f2.readline()
  k = f3.readline()
  print(i,j,k)
  #方法二:较为优雅
  with open('file1') as f1, open('file2') as f2, open('file3') as f3:
  for i in f1:
  j = f2.readline()
  k = f3.readline()
  print(i,j,k)
  ##方法三:通过contextlib模块的
  from contextlib import nested
  with nested(open('file1'), open('file2'), open('file3')) as (f1,f2,f3):
  for i in f1:
  j = f2.readline()
  k = f3.readline()
  print(i,j,k)
  4.2 文件的操作
  1.文件刚打开,指针在开始的地方,每次read ,readline ,readlines都会把指针移动到读取数据的地方
  2.seek()f.seek(0)#把指针移到文件开始
  3.tell()f.tell()#返回指针所在的位置
  4.3 文件的注意事项
  #'a'模式,每次都是从文件尾开始追加数据
  #注意文件的'w'模式,打开文件的时候就清空文件了。
  #打开大文件推荐使用readline()函数或者forlinein f
  files.read() 每次读取整个文件read可以传一个数字,表示读多少个字符。不传就是读整个文件
  files.readline()每只读取文件的一行
  files.readlines()每次按行读取整个文件内容,一次全部读取文件
  五、python其它基础知识
  5.1 元组
  元组:元组也可以嵌套任意类型的数据结构,但是元组不能修改,定义后不可改变,正是由于这种特性,元组这种数据类型可以作为字典的key,在nginx日志分析中用来统计多个维度(ip,url,code)等等。
  5.2 遍历list的方法
  1.直接遍历列表for i   inlist
  2. 根据索引值遍历range()配合长度
  3. 高阶遍历方法同时获取索引值和值
  In : for k ,v in enumerate(['one','two','three']):
  ...:   print k , v
  0 one
  1 two
  2 three
  5.3 遍历dict的方法(目前只有一种方法)
  fork ,v in dict.items():
  print k ,v
  六、函数
  函数的定义:简单的理解就是一次执行很多行代码
  格式:当前def缩进模块中的所有内容都是函数内容
  def 函数名(参数):
  函数内容
  6.1 简单的函数举例
  ##python中简单的函数
  In : def hello():
  ...:   print "hello world"
  ...:   print "hi Bob"
  ...:
  In : hello()
  hello world
  hi Bob
  6.2 函数的返回值
  ###函数的返回值:定义,即为函数执行后的状态结果,与shell中的echo $?相等
  print   hanshu   ==>函数功能、方法
  print   hanshu() ===>函数的返回值
  ##代码练习
  ##定义函数hello()
  In : def hello():
  ...:   print "hello world"
  ...:   print "hi Bob"
  ...:   return '2016'
  ...:
  ##直接print+函数名的结果 ===>返回结果为对象
  In : print hello
  
  ##print + 函数名()的结果 ===>返回函数的执行状态结果
  In : print hello()
  hello world
  hi Bob
  2016
  6.3 函数的参数:
  python中函数参数的种类1.必备参数2.关键字参数3.缺省值参数
  1.必备参数:调用函数时,必须以正确的顺序传入,并且传入的参数必须与声明的参数一致,否则可能出现错误参数调用的情况。
  2.关键字参数:关键字参数和函数调用关系紧密,函数调用使用关键字参数来确传入的参数值,使用关键字参数允许调用函数时参数的顺序和声明的时候不一致,因为python解释器能够用参数名匹配参数值。关键字参数的优点就是===>可读性好
  3.缺省值参数:调用函数时,如果缺省值参数没有传入的话,就是用预先定义的默认值。
  ###带参数的函数练习
  ##函数参数之一:必备参数
  In : def hello(name,age):
  ...:   print "hello %s,you age is %s" %(name,age)
  ...:   return '2016'
  ...:
  ...:
  In : hello('test',13)
  hello test,you age is 13
  Out: '2016'写一个阶乘的函数
  In : def jiecheng(num):
  ...:   res = 1
  ...:   while num > 0:
  ...:         res *= num
  ...:         num -= 1
  ...:   return res
  ...:
  In : jiecheng(3)
  Out: 6
  In : jiecheng(4)
  Out: 24
  ##函数参数之二:关键字参数
  In : def hello(name='aaa',age=12):
  ...:   print "hello %s,you age is %s" %(name,age)
  ...:   return '2016'
  ...:
  ...:
  In :
  In : hello(age=14,name='ccc')
  hello ccc,you age is 14
  Out: '2016'
  ##函数参数之三:缺省值参数
  In : def hello(name='aaa',age=12):
  ...:   print "hello %s,you age is %s" %(name,age)
  ...:   return '2016'
  ...:
  ...:
  In :
  In : hello(name='ccc')
  hello ccc,you age is 14
  Out: '2016'
  特殊参数情况:
  当函数希望收集不定长的参数时,即参数的个数不确定,则使用星号(*)来收集参数。星号(*)收集函数前边固定参数后剩下的所有参数。
  若函数收集的关键字参数也是个数不确定的话,则使用两个星号(**)来收集参数。
  ##特殊参数的代码详情
  ##一个星号(*)收集不定长参数
  ##情况一:注意*号收集前边参数收集后剩下的所有的参数
  In : def test(name,*num):
  ...:   print num
  In : test(1,2,3,4,5)
  (2, 3, 4, 5)
  ##情况二:收集所有的参数
  n : def add_all(*num):
  ...:   sum = 0
  ...:   for i in num:
  ...:         sum += i
  ...:   return sum
  ...:
  In : add_all(1,2,3)
  Out: 6
  In : add_all(1,2,3,4,5,6)
  Out: 21
  ##两个星号(**)收集关键字的参数
  ##(**)开头,收集关键字参数
  In : def test(**arr):
  ...:   print arr
  In : test(name='aa',age=15)
  {'age': 15, 'name': 'aa'}
  6.4 函数的进阶,使用函数优化之前的单词统计代码
  python的核心思想是函数化编程,所以函数式拆分代码需要注意;尽量避免全局变量,尽量减少函数间的练习,提高函数的可读性。程序是给人读的。
  ###拆分代码为函数的注意事项:尽量避免全局变量
  本人拆分统计单词读写文件的代码如下:
  #coding:utf-8
  res = {'!': 1, '.': 1, 'Bob': 1, 'I': 2, 'a': 1, 'am': 2, 'boy': 1}
  new = {}
  for k ,v in res.items():
  new.setdefault(v,[])
  new.append(k)
  print new
  #keys = max(new)
  #print "%s----> %s" %(keys,new)
  def op_file(filename,mode='r'):
  f = open(filename,mode)
  str = "/usr/local/src/newbie/03==>result\n\
  timesword"
  while True:
  keys = max(new)
  for i in new:
  str += "%s%s" %(keys,i)
  new.pop(keys)
  if len(new) == 0 :
  break
  str += "\n"
  if ('w' in mode) or ('a' in mode) :##使用in来判断模式是否为写模式
  f.write(str)
  f.close()
  op_file(filename='/var/www/html/tongji.html',mode="w+")
  6.5 变量的作用域
  讲到函数不得不引申下变量的作用域问题,一般编程语言中变量都分为全局变量和局部变量。具体定义自己可以百度了解些许,此处一带而过,python中全局变量使用关键词global来定义,一般在函数内部定义全局变量。
  ##global定义全局变量
  # cat a.py
  num = 3
  def hello():
  global num
  num = 5
  print num
  print hello()
  print "num = %s" %num
  # python a.py
  5
  None
  num = 5
  七、python语言中关于排序的进阶
  简而言之,冒泡排序、插入排序等一系列算法仅仅是编程语言的基础原理,而在python中可以通过简单的一两行代码实现复杂数据结构的排序。接下来将一步一步深入python排序原理及通间易读代码的实现。
  7.1 以nginx日志统计完ip后得到的结果字典为例
  res = {'111.85.41.230': 103, '218.200.66.197': 21, '218.200.66.196': 14, '221.176.5.153': 31, '218.200.66.199': 19, '218.200.66.198': 25, '221.176.5.156': 3, '10.35.1.82': 61, '221.176.5.154': 3, '220.181.125.177': 1, '101.227.4.33': 2, '220.172.215.69': 1}
  思路有两种:
  1.将字典翻转,即以出现的次数为key,value值为ip,但是需要注意的时这种情况,需要将ip存放到事先准备的空列表中去,否则key为ip出现次数的话,若有两个ip出现次数相同,那么会出现值被覆盖的情况从而使结果有所偏差。翻转后使用max(dict)取出最大次数的value值列表,拼接成html网页元素,然后dict.pop(keys)去掉最大值的key以此轮训依次出现最大ip的情况。---ByPanda
  2.上述res字典可以通过用dict.items()将res字典转换为元组组成的列表,然后通过对列表中每个元素中代表次数的项进行排序。然后根据排序结果拼接html元素。--By woniiu
  接下来主要深入woniu的思想为主线从而引出最终主人公python自带的sorted()函数。
  7.1.1 冒泡法实现排序:
  tmp = res.items()
  tmp = [('218.200.66.197', 21), ('218.200.66.196', 14), ('218.200.66.199', 19), \
  ('218.200.66.198', 25), ('101.227.4.33', 2), ('111.85.41.230', 103), \
  ('220.181.125.177' , 1), ('220.172.215.69', 1),('221.176.5.153', 31), \
  ('221.176.5.156', 3), ('10.35.1.82', 61), ('221.176.5.154', 3)]
  ##原始的冒泡排序代码:
  d = {'10.35.1.82': 61, '101.227.4.33': 2, '111.85.41.230': 103,'218.200.66.196': 14,\
  '218.200.66.197': 21,'218.200.66.198': 25,'218.200.66.199': 19,'220.172.215.69': 1,\
  '220.181.125.177': 1,'221.176.5.153': 31,'221.176.5.154': 3,'221.176.5.156': 3}
  tmp = d.items()
  print tmp
  le = len(tmp)
  for i in range(le-1):
  for j in range(le-1-i):
  if tmp < tmp:
  tmp,tmp = tmp,tmp
  print "sorted tmp :%s" %tmp
  ##排序后通过切片获取自己想要打印的数据。
  tmp = tmp[:3]
  for i in tmp:
  print "ip is %s ,count is %s" %i
  7.1.2 通过函数方法实现冒泡排序;
  用函数实现冒泡排序bubble_sort()
  ###代码:
  def bubble_sort(tmp,le,num=3):
  for i in range(le-1):
  for j in range(le-1-i):
  if tmp < tmp:
  tmp,tmp = tmp,tmp
  tmp = tmp[:num]
  return tmp
  le = len(tmp)
  res = bubble_sort(tmp,le,num=4)
  7.3 函数的进阶
  ###进阶排序函数===>函数可以当作参数传递给另一个函数
  具体实现代码如下:
  ##以函数为参数传递给函数
  # cat sort_fn.py
  #coding:utf-8
  ##原始数据
  arr =
  ##列表嵌套元组
  arr2 = [('xiaohong',90),('xiaohua',58),('xiaohei',100)]
  ##列表嵌套字典
  d = [{'name':'xiaohong','age':18},{'name':'xiaohei','age':10},{'name':'xiaowang','age':33}]
  def sort_fn1(data):##取出需要排序的元素(针对原始数据)
  return data
  def sort_fn2(data):##取出需要排序的元素(针对列表)
  return data
  def sort_fn3(data):##取出需要排序的元素(针对字典)
  return data['age']
  ##自己定义的排序函数
  def my_sort(arr,sort_fn1):
  for j in range(len(arr)-1):
  for i in range(len(arr)-1):
  if sort_fn1(arr) >sort_fn1(arr):
  arr,arr=arr,arr
  return arr
  ##对原始数据进行排序
  print my_sort(arr,sort_fn1)
  ##对列表嵌套元组进行排序
  print my_sort(arr2,sort_fn2)
  ##对列表嵌套字典进行排序
  print my_sort(d,sort_fn3)
  # python sort_fn.py
  ##原始数据的排序结果
  
  ##列表嵌套元组的排序结果
  [('xiaohua', 58), ('xiaohong', 90), ('xiaohei', 100)]
  ##列表嵌套字典的排序结果
  [{'age': 10, 'name': 'xiaohei'}, {'age': 18, 'name': 'xiaohong'}, {'age': 33, 'name': 'xiaowang'}]
  Noneoot@xiaowei sorted]#
  7.4 最终大招--python自带的sorted()函数排序
  sorted()函数的格式如下:
  伪代码格式:    sorted(需要排序的列表,排序的元素)
  编码格式:    sorted(arr,key=函数)
  ###使用python自带的sorted函数代码
  ##代码示例
  # cat sorted.py
  arr =
  def sort_fn1(data):
  return data
  def sort_fn2(data):
  return data
  def sort_fn3(data):
  return data['age']
  print sorted(arr,key=sort_fn1)
  arr2 = [('xiaohong',90),('xiaohua',58),('xiaohei',100)]
  print sorted(arr2,key=sort_fn2)
  d = [{'name':'xiaohong','age':18},{'name':'xiaohei','age':10},{'name':'xiaowang','age':33}]
  print sorted(d,key=sort_fn3)
  ##执行结果如下:
  #
  # python sorted.py
  
  [('xiaohua', 58), ('xiaohong', 90), ('xiaohei', 100)]
  [{'age': 10, 'name': 'xiaohei'}, {'age': 18, 'name': 'xiaohong'}, {'age': 33, 'name': 'xiaowang'}]
  7.5 python函数必备高阶技能之---lambda匿名函数
  lambda匿名函数的定义:特别简单的、没有名字的函数,仅有函数return返回值的函数。
  ###lambda函数举例
  ##lambda代码举例===>用于简写,简洁代码
  In : hello = lambda x:x
  In : print hello()
  
  In : hello = lambda x:x
  In : print hello()
  3
  7.6 sorted()函数 + lambda匿名函数 终极通间易读实现排序
  ####思路就是将sorted()函数的key用lambda来定义
  ##代码举例
  # cat ver.py
  arr =
  arr2 = [('xiaohong',90),('xiaohua',58),('xiaohei',100)]
  d = [{'name':'xiaohong','age':18},{'name':'xiaohei','age':10},{'name':'xiaowang','age':33}]
  print sorted(arr,key=lambda x:x)###使用lambda来获取排序的对象
  print sorted(arr2,key=lambda x:x)###使用lambda来获取排序的对象
  print sorted(d,key=lambda x:x['age'])###使用lambda来获取排序的对象
  ###排序结果展示
  # python ver.py
  
  [('xiaohua', 58), ('xiaohong', 90), ('xiaohei', 100)]
  [{'age': 10, 'name': 'xiaohei'}, {'age': 18, 'name': 'xiaohong'}, {'age': 33, 'name': 'xiaowang'}]
  7.7 nginx日志处理的最终优化代码
  https://github.com/Fuliwei/newbie/blob/master/04/ketang/test.py(最终代码在辛苦移步github)
  ###统计nginx日志最终进阶:
  ##排序时使用sorted()+lambda以快速实现
  ###统计ip以及出现次数排序的代码优化
  # cat test.py
  #/usr/bin/python
  #coding:utf-8
  def htmlStr(arr):
  html_str = "/usr/local/src/newbie/04/ketang/test.py ====>result\n\n%s%s%s\n" %('Count','IP','URL')
  for i in arr:
  html_str += "%s%s%s\n" %(i,i,i)
  html_str += "\n"
  return html_str
  def operateFile(filename,mode='r',string=''):
  if ('w' in mode) or ('a' in mode):
  with open(filename,'w') as files:
  files.write(string)
  return 'Write OK'
  else:
  res = {}
  with open(filename) as files:
  for line in files:
  #print line
  line = line.strip('\n').split(' ')
  #print line
  tmp = (line,line)#,line)
  res = res.get(tmp,0)+1
  #print "res is %s" %res
  return sorted(res.items(),key=lambda x:x,reverse = True)
  test = operateFile('log.log')
  test = test[:10]
  htmlstr = htmlStr(test)
  print operateFile('/var/www/html/test.html',mode='w',string=htmlstr)
  --end Thx.

页: [1]
查看完整版本: 从零开始学Python-day5