定义一个生成器函数,生成1-10,使用迭代器模拟range的功能,自己建立一个range:MyRange,re中函数的使用(自己写用例来使用):

1.定义一个生成器函数,生成1-10

使用next(generator)方法获取1-10
使用for循环获取

def get_num():
    for num in range(1, 11):
        yield num


test = get_num()
print(type(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))

结果:

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

range(10)
range(1, 10)
range(1, 10, 1) => start, stop, step
range(10, 1, -1)
range(10, -1, -1)
range(-10, -1, 1)
range(-1, -10, -1)

class MyRange:

    def __init__(self, *args):
        if len(args) == 1:
            self.start = 0
            self.stop = args[0]
            self.step = 1
        if len(args) == 2:
            self.start, self.stop = args
            self.step = 1
        if len(args) == 3:
            self.start, self.stop, self.step = args
            if self.step == 0:
                raise ValueError('range() arg 3 must not be zero')
        # print(args, type(args))
        # print(self.start, self.stop, self.step)

    def __iter__(self):
        return self

    def __next__(self):
        data = self.start
        if self.step > 0:
            if self.start < self.stop:
                self.start += self.step
                return data
            else:
                raise StopIteration
        if self.step < 0:
            if self.start > self.stop:
                self.start += self.step
                return data
            else:
                raise StopIteration

    pass


print(list(MyRange(10)))
print(list(MyRange(1, 10)))
print(list(MyRange(1, 10, 1)))
print(list(MyRange(10, 1, -1)))
print(list(MyRange(10, -1, -1)))
print(list(MyRange(-10, -1, 1)))
print(list(MyRange(-1, -10, -1)))

结果:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 9, 8, 7, 6, 5, 4, 3, 2]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[-10, -9, -8, -7, -6, -5, -4, -3, -2]
[-1, -2, -3, -4, -5, -6, -7, -8, -9]

3. re中函数的使用(自己写用例来使用)

3.1 match

import re
pattern = "hello"
string = "hello world"
result = re.match(pattern, string)
print(result, type(result))

<re.Match object; span=(0, 5), match='hello'> <class 're.Match'>

3.2 fullmatch

import re
pattern = "hello"
string = "hello"
match_obj = re.fullmatch(pattern, string)
print(match_obj)

<re.Match object; span=(0, 5), match='hello'>

3.3 search

pattern = "hello"
string = "world hello"
match_obj = re.search(pattern, string)
print(match_obj)

<re.Match object; span=(6, 11), match='hello'>

3.4 findall

string3 = "hello world hello"
pattern = "hello"
result = re.findall(pattern, string3)
print(result)

['hello', 'hello']

3.5 finditer

string = "hello world hello"
pattern = "hello"
result = re.finditer(pattern, string)
print(result)

<callable_iterator object at 0x000002922BB52BE0>

3.6 split

string = "计算机,软件,网络"
pattern = ","
result = re.split(pattern, string, maxsplit=1)
print(result)

['计算机', '软件,网络']

3.7 sub

result = re.subn(",", "-", "计算机,软件,网络", )
print(result)
result = re.subn(",", "-", "计算机,软件,网络", 1)
print(result)

计算机-软件-网络
计算机-软件,网络

3.8 subn

result = re.subn(",", "-", "计算机,软件,网络", 1)
print(result)
result = re.subn(",", "-", "计算机,软件,网络",)
print(result)

('计算机-软件,网络', 1)
('计算机-软件-网络', 2)

3.9 complie

string = "hello world hello"
pattern = "hello"
compile_obj = re.compile(pattern)
print(compile_obj.search(string))
print(compile_obj.findall(string))
print(compile_obj.match(string))

<re.Match object; span=(0, 5), match='hello'>
['hello', 'hello']
<re.Match object; span=(0, 5), match='hello'>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Golang_HZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值