Python入门:生成器,模拟range功能,re模块

目录

一.定义一个生成器

二.模拟range函数的功能,自己建立一个range:MyRange

三.re模块中函数的使用


一.定义一个生成器

生成器函数:含有yield语句的函数

调用生成器函数的对象就是生成器

要求:生成1-10,使用next(generator)方法获取1-10,使用for循环获取

def data():
    for i in range(1, 11):
        yield i
 
 
generator = data()
print(next(generator))
print(next(generator))
print(next(generator))
print(next(generator))
print(next(generator))
for j in generator:
    print(j)
 
#执行结果
1
2
3
4
5
6
7
8
9
10

二.模拟range函数的功能,自己建立一个range:MyRange

class MyRange:
    def __init__(self, start, stop=None, step=1):
        if step == 0:
            raise ValueError('range() arg 3 must not be zero')
        elif stop is None:
            start, stop = 0, start
        elif step > 0:
            self.__length = max(0, ((stop - start - 1)//step)+1)
        else:
            self.__length = max(0, ((start - stop - 1)//abs(step))+1)
        self.__start = start
        self.__stop = stop
        self.__step = step
 
    def __iter__(self):
        num = self.__start
        if self.__step > 0:
            while num < self.__stop:
                yield num
                num += self.__step
        else:
            while num > self.__stop:
                yield num
                num += self.__step
 
    def __len__(self):
        return self.__length
 
    def __getitem__(self, k):
        if k < 0:
            k += self.__length
        if 0 <= k < self.__length:
            return self.__start + k*self.__step
        else:
            raise IndexError('range object index out of range')

三.re模块中函数的使用

1.match()

决定re是否在字符串刚开始的位置匹配

import re
str_data = "happy,happy"
pattern = 'happy'
match_obj = re.match(pattern, str_data)
print(match_obj)
 
#执行结果

这个方法并不是完全匹配,当pattern结束时若string还有剩余字符,仍然视为成功

2.fullmatch()

如果整个string匹配到正则表达式样式,就返回一个相应的匹配对象 。 否则就返回一个None

import re

str_data = "happy,happy"
pattern = 'happy,happy'
match_obj = re.fullmatch(pattern, str_data)
print(match_obj)
 
#执行结果



str_data = "happy,happy"
pattern = 'happy'
match_obj = re.fullmatch(pattern, str_data)
print(match_obj)

#执行结果
None

3.search()

搜索,没有指定从什么地方开始搜索, 从任意位置开始匹配,只匹配第一次找到的

str_data = "what"
pattern = 'w'
match_obj = re.search(pattern, str_data)
print(match_obj)
 
#执行结果

4.findall()

def findall(pattern, string, flags=0):

查找所有,以列表形式返回

str_data = "happy"
pattern = 'p'
match_obj = re.findall(pattern, str_data)
print(match_obj)
 
#执行结果
['p', 'p']

finditer()

def finditer(pattern, string, flags=0):

也是查找所有,只是将查找的结果的match object放入迭代器中

str_data = "happy"
pattern = 'a'
match_obj = re.finditer(pattern, str_data)
print(match_obj.__next__())

 
#执行结果

5.split()

def split(pattern, string, maxsplit=0, flags=0):

按照正则表达式进行分割,返回一个列表

str_data = "welcome to xi'an"
pattern = ' '
match_obj = re.split(pattern, str_data)
print(match_obj)
 
#执行结果
['welcome', 'to', "xi'an"]

6.sub()

替换

def sub(pattern, repl, string, count=0, flags=0):

count:替换次数

str_data = "happy"
pattern = 'p'
match_obj = re.sub(pattern, '*', str_data, 1)
print(match_obj)

#执行结果
ha*py

7.subn()

替换

def subn(pattern, repl, string, count=0, flags=0):

count: 替换次数

返回一个元组

str_data = "happy"
pattern = 'a'
match_obj = re.subn(pattern, 'y', str_data, 1)
print(match_obj)

#执行结果
('hyppy', 1)

8.complie()

编译,用来生成正则表达式对象

def compile(pattern, flags=0):

str_data = "welcome to xi'an"
pattern = 't'
match_obj = re.compile(pattern)
print(match_obj.search(str_data))
 
#执行结果

你可能感兴趣的:(python,pycharm,学习)