Python3高级基础(2) 1 Python3模拟数据结构 2 迭代器(iterator) 3 生成器 4 图片处理例子 5 Python3开发面试题 引用文献

1.1 栈

栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表。栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进栈)。

栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。

1.进栈(PUSH)算法

①若TOP≥n时,则给出溢出信息,作出错处理(进栈前首先检查栈是否已满,满则溢出;不满则作②);

②置TOP=TOP+1(栈指针加1,指向进栈地址);

③S(TOP)=X,结束(X为新进栈的元素);

2.退栈(POP)算法

①若TOP≤0,则给出下溢信息,作出错处理(退栈前先检查是否已为空栈, 空则下溢;不空则作②);

②X=S(TOP),(退栈后的元素赋给X):

③TOP=TOP-1,结束(栈指针减1,指向栈顶)

lst=[]
 
def pop():
    if(len(lst)==0):
        print("栈为空","无法出栈")
    else:
        print("此次出栈元素为:",lst.pop())
 
 
def push(i):
    lst.append(i)

    
push(1)
push(2)
push(3)
pop()
pop()
pop()
pop()
此次出栈元素为: 3
此次出栈元素为: 2
此次出栈元素为: 1
栈为空 无法出栈

1.2 队

像栈一样,队列(queue)也是一种线性表,它的特性是先进先出,插入在一端,删除在另一端。就像排队一样,刚来的人入队(push)要排在队尾(rear),每次出队(pop)的都是队首(front)的人。如图1,描述了一个队列模型。

队列(Queue)与栈一样,是一种线性存储结构,它具有如下特点

  • 队列中的数据元素遵循“先进先出”(First In First Out)的原则,简称FIFO结构。
  • 在队尾添加元素,在队头删除元素。
lst=[]
 
def enpush(i):      # 入队操作
    lst.append(i)

def enpop():        # 出队操作
    if(len(lst)==0):
        print("队列为空,无法出队")
    else:
        print("出队元素为:",lst.pop(0))
 
 
enpush(10)
enpush(20)
enpush(2)
 
enpop()
enpop()
enpop()
enpop()
出队元素为: 10
出队元素为: 20
出队元素为: 2
队列为空,无法出队

2 迭代器(iterator)

  • 特点

    节省内存
    比如展现一个上亿元素的列表并计算,sum([i*i for i in range(1000000000)])
    内存小的机器肯定要宕机
    惰性机制
    调用一次__next__方法,执行一次
    单向执行,不可逆
    既就是迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完后结束,只能往前不能后退。

2.1 判断对象是不是可迭代对象

常见的迭代对象:包含**字符串str、列表list、元组tuple、字典dict、集合set、range以及文件句柄**

1. 第一种判断方法__iter__
myStr = 'bright'
myList = [1,2,3,4,5]
print('__iter__' in dir(myStr))
print('__iter__' in dir(myList))
True
True


2. 第二种判断方法 isinstance('abc',Iterable)
myStr = "bightYU"
myList = [1,3,4,5,6,7]
from collections import Iterable
print(isinstance(myStr,Iterable))
print(isinstance(myList,Iterable))
print(isinstance(123,Iterable))
True
True
False

2.2 创建迭代器的方法

  • 将可迭代对象转化为迭代器

    1. 第一种方法__iter__()
str1="abcdegggg"
list1=[1,3,4,5]
obj_str=str1.__iter__()
obj_list=list1.__iter__()
print(obj_str)
print(obj_list)
<str_iterator object at 0x000001CA66A4ECC0>
<list_iterator object at 0x000001CA66A4EEF0>


2. 第二种方法iter()
str1="abcdegggg"
list1=[1,3,4,5]
obj_str=iter(str1)
obj_list=iter(list1)
print(obj_str)
print(obj_list)
<str_iterator object at 0x000001CA66A4EF60>
<list_iterator object at 0x000001CA667A6CC0>

2.3 判断对象是不是迭代器

- 内部含有__iter__ 且含有__next__方法的对象就是迭代器,遵循迭代器协议
str1="abcdegggg"
list1=[1,3,4,5]
obj_str=str1.__iter__()
obj_list=list1.__iter__()

print('__iter__' in dir(str1))
print('__next__' in dir(str1))

print('__iter__' in dir(obj_str))
print('__next__' in dir(obj_str))
True
False
True
True


可以看到,str1虽然是可迭代对象,但是内部不含有__next__方法,所以不是迭代器

- isinstance(obj_list,Iterable)
from collections import Iterable
str1="abcdegggg"
list1=[1,3,4,5]
obj_str=str1.__iter__()
obj_list=list1.__iter__()
print(isinstance(list1,Iterable))
print(isinstance(obj_list,Iterable))
print(isinstance(124,Iterable))
True
True
False

3 生成器

在Python中,使用了yield的函数被称为生成器(generator)

跟普通函数不同的是,生成器是一个返回迭代器的函数,只能用于迭代操作,等简单的理解生成器就是一个迭代器。在调用生成器的运行的过程中,每当遇到yield时函数会暂停并保存当前所有的运行信息,返回yield的值,并在下一次执行next()方法是从当前暂停的位置继续运行。
调用一个生成器,返回的是一个迭代器对象。

3.1 创建生成器

def scq():
    print('11')
    yield print('22')
    yield print('33')
    yield print('44')
r = scq()
print(type(r), r)
<class 'generator'> <generator object scq at 0x000001CA66A15FC0>
ret = r.__next__()
11
22
'''
下面示例中gener_obj=func1()为生成器对象
next() 和 yield 必须一一对应
每调用一次__next__()执行一个yield
如果__next__()多于yield则会报错
'''
def func1():
    print(1111)
    yield 1
    print(2222)
    yield 2
    print(3333)
    yield 3
    print(4444)
    
gener_obj=func1()
print(gener_obj.__next__())
print(gener_obj.__next__())
print(gener_obj.__next__())

1111
1
2222
2
3333
3

3.2 生成器例子

def cloth():
    for i in range(1,1000):
        yield '衣服%s'%i
ger_obj=cloth()
for i in range(3):
    print('结果:', ger_obj.__next__())
结果: 衣服1
结果: 衣服2
结果: 衣服3
def cloth():
    for i in range(1,1000):
        yield '衣服%s'%i
        
ger_obj=cloth()

for i in range(3):
    print(ger_obj.__next__())
    
print('-'*20)

for i in range(5):
    print(ger_obj.__next__())
衣服1
衣服2
衣服3
--------------------
衣服4
衣服5
衣服6
衣服7
衣服8

4 图片处理例子

import cv2
img = cv2.imread('pythontab.jpg', cv2.IMREAD_COLOR) # 彩色图模式加载一副彩图
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('lena.png', img)
True

5 Python3开发面试题

【引自】http://www.cnblogs.com/ManyQian/p/9152002.html

题目:

编写Python脚本,分析xx.log文件,按域名统计访问次数
xx.log文件内容如下:
https://www.sogo.com/ale.html
https://www.qq.com/3asd.html
https://www.sogo.com/teoans.html
https://www.bilibili.com/2
https://www.sogo.com/asd_sa.html
https://y.qq.com/
https://www.bilibili.com/1
https://dig.chouti.com/
https://www.bilibili.com/imd.html
https://www.bilibili.com/

输出:
4 www.bilibili.com
3 www.sogo.com
1 www.qq.com
1 y.qq.com
1 dig.chouti.com

解题思路:

1、先获取数据就是域名  

获取数据我们可以用正则,或者域名还是有相同点可以用split切分

2、统计域名访问的次数

可以用Python的内置模块来统计,

3、然后就是输出要求的格式

sorted内置函数用来排序
import re
from collections import Counter
with open("xx.log","r",encoding="utf-8") as f:
    data=f.read()
    res=re.findall(r"https://(.*?)/.*?", data)
    dic=Counter(res)
      
ret = sorted(dic.items(), key=lambda x:x[1], reverse=True)

for k,v in ret:
    print(v,k)
4 www.bilibili.com
3 www.sogo.com
1 www.qq.com
1 y.qq.com
1 dig.chouti.com

引用文献