目的

掌握正则与过滤基本语法

实现

代码练习

代码

01.filter

  list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  
  
  def num(onum):
      if onum % 2 == 0:
          return True
      return False
  
  
  l = filter(num, list1)
  print(list(l))
=====02.sort=====

        list = [1, 4, 67, 9, 0, -1, 2]
      
      listSort = sorted(list, key=abs)
      
      print(list)
      print(listSort)
      
      
      list1 = [1, 4, 67, 9, 0, -1, 2]
      
      listSort1 = sorted(list, reverse=True)
      
      print(list1)
      print(listSort1)

02.unittest

  import unittest
  import Test
  import persion
  
  
  class myTest(unittest.TestCase):
  
      def setUp(self):
          print("Test is start")
  
      def tearDown(self):
          print("Test is end")
  
      def test_sum(self):
          self.assertEqual(Test.sum0(1, 2), 3, "加法有误")
  
      def test_sum1(self):
          self.assertEqual(Test.sum1(2, 2), 4, "乘法有误")
  
      def test_name(self):
          p = persion.Person("davis", 20)
          self.assertEqual(p.name, "davis", "名字错误")
  
      def test_age(self):
          p = persion.Person("davis", 20)
          self.assertEqual(p.getAge(), p.age, "年龄错误")
  
  
  if __name__ == "__main__":
      unittest.main()

03.排列与组合

  import itertools
  
  # 排列
  myList = list(itertools.permutations([1, 2, 3, 4, 5], 3))
  print(myList)
  print(len(myList))
  print(myList[3])
  
  # 组合
  mylist1 = list(itertools.combinations([1, 2, 3, 4, 5], 4))
  print(mylist1)
  print(len(mylist1))
  print(mylist1[3])
  
  # 排列组合
  mylist2 = list(itertools.product("0123456789", repeat=3))
  print(mylist2)
  print(len(mylist2))
  
  # 迭代器取没一行数据
  passw= ("".join(x) for x in itertools.product("01234678", repeat=3))
  
  while True:
      try:
          newpass = next(passw)
          print(newpass)
      except StopIteration as e:
          break
  

04.正则

  import re
  
  # re.match 查找开始第一个,返回匹配
  print(re.match("www", "www.davis-wiki.com"))
  print(re.match("www", "ww.davis-wiki.com"))
  print(re.match("www", "ww.davis-wiki.wwwcom"))
  print(re.match("wwW", "www.davis-wiki.com"))
  print(re.match("wwW", "www.davis-wiki.com", re.I))
  # re.search 查找整个,返回第一个
  print(1, re.search("www", "ww.davis-wiki. wwwcom"))
  print(2, re.search("www", "www.davis-wiki.com"))
  
  # re.findall 查找整个,返回所有匹配list
  print(re.findall("www", "www.davis-wiki.com"))
  print(re.findall("www", "www.daviswww-wiki.com"))
  print(re.findall("www", "www.davis-wikiwww.comwww"))
  print(re.findall("www", "www.wwwdavis-wiki.com"))
  
  # re.split 空格分割(忽略空格的数量)
  str1 = "www   wwwdavis-wiki  com"
  print(str1.split(" "))
  print(re.split(r" +", str1))
  
  # re.finditer 扫描全部数据,返回迭代器
  str2 = "davis love you! davis love you! davis love you!"
  fd = re.finditer("davis", str2)
  while True:
      try:
          f = next(fd)
          print(f)
      except StopIteration as e:
          break
  
  # re.sub("查找的字符",“替换的字符”,“全部内容”,“替换数量”,“flag忽略大小写之类的参数(re.I,.........)”),
  # 返回修改后的字符串
  str3 = "davis love you! davis love you! davis love you!"
  print(re.sub("love", "hate", str3))
  print(re.sub("love", "hate", str3, 2))
  
  # re.subn("查找的字符",“替换的字符”,“全部内容”,“替换数量”,“flag忽略大小写之类的参数(re.I,.........)”),
  # 返回修改后的串+被替换的个属
  str3 = "davis love you! davis love you! davis love you!"
  print(re.subn("love", "hate", str3))
  print(re.subn("love", "hate", str3, 2))
  
  # group (块)[?P<>块名]
  tel = "010-23467890"
  gr = re.match(r"(?P<first>\d{3})-(?P<last>\d{8})", tel)
  print(gr.group(0))
  print(gr.group(1))
  print(gr.group(2))
  print(gr.groups())
  print(gr.group("first"))
  print(gr.group("last"))
  
  # re.compile编译
  pat = r"^1(([3578]\d)|([47]))\d{8}$"
  print(re.match(pat, "13600000000"))
  re.telehpone = re.compile(pat)
  print(re.telehpone.match("13600000000"))