Python 列表解析

列表解析

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

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

语法:

[expression for iter_val in iterable]

[expression for iter_val in iterable if cond_expr]

实例展示:


 要求:列出1~10所有数字的平方
 ####################################################
 1、普通方法:
 >>> L = []
 >>> for i in range(1,11):
 ...     L.append(i**2)
 ... 
 >>> print L
 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
 ####################################################
 2、列表解析
 >>>L = [ i**2 for i in range(1,11)]
 >>>print L
 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
要求:列出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]
要求:列出1~10所有数字的平方除以2的值
####################################################
1、普通方法
>>> L = []
>>> for i in range(1,11):
...     L.append(i**2/2)
... 
>>> print L
[0, 2, 4, 8, 12, 18, 24, 32, 40, 50]
####################################################
2、列表解析
>>> L = [i**2/2 for i in range(1,11) ]
>>> print L
[0, 2, 4, 8, 12, 18, 24, 32, 40, 50]
要求:列出"/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)]
使用列表解析生成 9*9 乘法表
 
print('\n'.join([''.join(['%s*%s=%-2s '%(y,x,x*y)for y in range(1,x+1)])for x in range(1,10)]))
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值