python 迭代方法及列表解析(列表推导式) - Go语言中文社区

python 迭代方法及列表解析(列表推导式)


知识补充

for 循环

for循环一般会访问一个可迭代的对象(如序列或者是迭代器),并且在所有条目都处理过之后结束循环。
 

for ivalue in (1,2,3)(迭代器):     

    print ivalue 

每次循环, ivalue 迭代变量被设置为可迭代对象的当前元素。

for语句循环特点:

(1)通常用于遍历序列成员:字符串、列表、元组和字典;

(2)会自动的调用迭代器的next()方法,捕获StopIteration异常并结束循环;

  • for语句对容器对象调用iter()函数,iter()是Python内置函数。
  • iter()函数会返回一个定义了next()方法的迭代器对象,它在容器中逐个访问容器内的元素。
  • next()也是Python内置函数。在没有后续元素时,next()会抛出一个StopIteration异常,通知for语句循环结束。

 

迭代器

  • 它是一个带状态的对象,能在调用next()方法的时候返回容器中的下一个值。    
  • 任何实现了__iter__和__next__() 方法的对象都是迭代器,__iter__返回迭代器自身,__next__返回容器中的下一个值。

 

python迭代方法

通过序列项迭代

通过序列索引迭代

通过项和索引迭代

 

内建函数range()用法:   

range( start , end, step = 1 )     

range()会返回一个包含所有k的列表:         

      (1)start <= k < end         

      (2)k每次递增step,step不能为0,否则发送错误     

range()简略用法:         

range( end )    (start默认为0,step为1)         

range( start , end )

 

同时迭代两个序列

 

内建函数zip

内建函数zip用来进行并行迭代,可用作多个序列(能够处理不等长,最短原则)

 

列表解析

  根据已有列表,高效创建新列表的方式。

  列表解析是Python迭代机制的一种应用,它常用于实现创建新的列表,因此用在[]中。

语法:

  [expression for iter_val in iterable]

  [expression for iter_val in iterable if cond_expr]

实例展示:

要求:列出1~10中大于等于4的数字的平方
####################################################
1、普通方法:
>>> L = []
>>> for i in range(1,11):
...     if i >= 4:
...         L.append(i**2)
... 
>>> print L
[16, 25, 36, 49, 64, 81, 100]
####################################################
2、列表解析
>>>L = [ i**2 for i in range(1,11) if i >= 4 ]
>>>print L
[16, 25, 36, 49, 64, 81, 100]

 

要求:列出"/var/log"中所有已'.log'结尾的文件
##################################################
1、普通方法
>>>import os
>>>file = []
>>> for file in os.listdir('/var/log'):
...     if file.endswith('.log'):
...         file.append(file)
... 
>>> print file
['anaconda.ifcfg.log', 'Xorg.0.log', 'anaconda.storage.log', 'Xorg.9.log', 'yum.log', 'anaconda.log', 'dracut.log', 'pm-powersave.log', 'anaconda.yum.log', 'wpa_supplicant.log', 'boot.log', 'spice-vdagent.log', 'anaconda.program.log']
##################################################
2.列表解析
>>> import os
>>> file = [ file for file in os.listdir('/var/log') if file.endswith('.log') ]
>>> print file
['anaconda.ifcfg.log', 'Xorg.0.log', 'anaconda.storage.log', 'Xorg.9.log', 'yum.log', 'anaconda.log', 'dracut.log', 'pm-powersave.log', 'anaconda.yum.log', 'wpa_supplicant.log', 'boot.log', 'spice-vdagent.log', 'anaconda.program.log']
要求:实现两个列表中的元素逐一配对。
1、普通方法:
>>> L1 = ['x','y','z']
>>> L2 = [1,2,3]      
>>> L3 = []
>>> for a in L1:
...     for b in L2:
...         L3.append((a,b))
... 
>>> print L3
[('x', 1), ('x', 2), ('x', 3), ('y', 1), ('y', 2), ('y', 3), ('z', 1), ('z', 2), ('z', 3)]
####################################################
2、列表解析:
>>> L1 = ['x','y','z']
>>> L2 = [1,2,3]
L3 = [ (a,b) for a in L1 for b in L2 ]
>>> print L3
[('x', 1), ('x', 2), ('x', 3), ('y', 1), ('y', 2), ('y', 3), ('z', 1), ('z', 2), ('z', 3)]

 

 

 

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/fireflylane/article/details/84672199
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-18 20:36:29
  • 阅读 ( 839 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢