• 列表解析


    迭代器就是一个有.next()方法的对象,而不是通过索引来计数。
    迭代器的限制:不能回退!

    使用迭代器:
    >>> develop = ('python','c#','ruby','php','java')
    >>> progarmme = iter(develop) #定义迭代器
    >>> progarmme.__next__() #使用迭代器方法1
    'python'
    >>> next(progarmme) #使用迭代器方法2
    'c#'
    >>> progarmme.__next__()
    'ruby'
    >>> next(progarmme)
    'php'
    >>>

    迭代到最后会怎样?
    >>> next(progarmme)
    'java'
    >>>
    >>>
    >>> next(progarmme)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    StopIteration

    返回一个Traceback,提示终止迭代。

    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-
    
    develop = ['python','ruby','C#','Java','PHP']
    
    
    programme = iter(develop)
    
    while True:
    try:
    i = programme.__next__()
    except StopIteration:
    break
    print(i)

    当然,这也的行为显然没有下面的语句高效:
    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-

    develop = ['python','ruby','C#','Java','PHP']


    for i in develop:
    print(i)


    在迭代器产生之前,如何逐行读取文件?
    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-

    with open('OSPF可选项字段.txt','r',encoding='UTF-8') as myfile:
    for eachline in myfile.readlines():
    print(eachline)


    有了迭代器之后,还可以这样读取:
    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-

    with open('OSPF可选项字段.txt','r',encoding='UTF-8') as myfile:
    for eachline in myfile:
    print(eachline)


    充分表明:for语句内嵌迭代机制!

    列表解析(list comps)——动态创建列表

    列表解析的语法:

    [expr for iter_var in iterable]

    这语句的核心是for循环,迭代iterable对象的所有条目,前面的表达式(expr)应用于序列的每个成员,最后的结果是产生新的列表。

    生成1,10所有数字的平方,保存到列表L中
    >>> L = [i**2 for i in range(1,11)]
    >>> L
    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


    计算文件最长行单词数量:
    范例文件:/etc/passwd

    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-
    
    file = open('passwd','r')
    
    longest = 0
    
    alllines = file.readlines()
    
    file.close()
    
    for line in alllines:
    linelen = len(line.strip())
    if linelen > longest:
    longest = linelen
    
    print(longest)

    通过列表解析完成工作:

    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-
    
    file = open('passwd','r')
    
    longest = 0
    
    
    alllines = [x.strip() for x in file.readlines()]
    
    file.close()
    
    for line in alllines:
    linelen = len(line.strip())
    if linelen > longest:
    longest = linelen
    
    print(longest)

    这两种方法的通病:
    readlines()会读取文件所有行,然后以空白字符为分隔符计算len,对于超大文件这显然是不合理的。

    优化:

    !/usr/bin/env python3
    #-*- coding:utf-8 -*-
    
    file = open('passwd','r')
    
    alllines = [len(x.strip()) for x in file]
    
    file.close()
    
    print(max(alllines))

    思路,就是迭代器可以读取迭代文件本身!!!

    这里还是有问题的,因为对文件迭代的时候,还是会把每一行读取到内存进行处理,然后在生成列表。

    对代码进行进一步简化:

    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-
    
    print(max(len(x.strip()) for x in open('passwd','r')))
  • 相关阅读:
    CMD指令
    六种Socket I/O模型幽默讲解
    性格与职业的选择
    为什么主引导记录的内存地址是0x7C00?
    pandas数据分析第二天
    pandas数据结构和介绍第一天
    tornado options
    tornado.web.StaticFileHandler
    mysql多条更新
    pandas
  • 原文地址:https://www.cnblogs.com/alben-cisco/p/6980513.html
Copyright © 2020-2023  润新知