当前位置:首页 » python教程 » 正文

Python中使用filter过滤列表的一个小技巧分享

看: 736次  时间:2020-07-13  分类 : python教程

有的时候使用dir(Module),可以查看里面的方法,但是模块自带的属性"__"开头的也会显示,如下:

>>> import random
>>> dir(random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_Buil
tinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_acos', '_c
eil', '_cos', '_e', '_exp', '_hashlib', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_
generator', '_urandom', '_warn', 'betavariate', 'choice', 'division', 'expovariate', 'gammavariate', 'gauss', 'getrandbi
ts', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sam
ple', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
>>>

这个时候想过滤以"_"或"__"开头的方法,可以:

>>> filter(lambda s: not s.startswith("_"), dir(random))
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', 'betav
ariate', 'choice', 'division', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormv
ariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 't
riangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
>>>

从上面来看,使用filter()函数,结合lambda函数很好的完成了任务。 其他的例子,比如想从一个列表中过滤非数字的字符串列表:

>>> L = ["1234", "ABCD", "BOOK"]
>>> filter(lambda s: s.isdigit(), L)
['1234']
>>>

补充知识:python不同长度列表,对应合并

1. 说明

lis1 = [{‘OS_bit': u'64 \u4f4d',
‘OS_version': ‘10.0.10240',
‘OS_name': u'Microsoft Windows 10 \u4f01\u4e1a\u7248 2015 \u957f\u671f\u670d\u52a1\u65b9\u6848'}]
lis2 = [{‘ip':‘10.20.122.32'}]
lis3 = [{‘CPU_name': u'Intel® Core™ i5-4200H CPU @ 2.80GHz'}]
lis4 = [{‘memory_size': ‘1600MHz',
‘memory_name': u'Physical Memory 0'},
{‘memory_size': ‘1600MHz',
‘memory_name': u'Physical Memory 2'}]
lis5 = [{‘GPU_name': u'NVIDIA GeForce GTX 950M',
‘GPU_size': ‘2G'},
{‘GPU_name': u'Intel® HD Graphics 4600',
‘GPU_size': ‘1G'}]

有这五个列表,要求合并成一个列表,并且所有列表的第一元素放在新列表的第一元素,以此类推。

2. 代码

# !/usr/bin/env/python
# _*_coding:utf-8_*_
# Data:2019-04-10
# Auther:苏莫
# Link:QQ2388873062
# Address:https://blog.csdn.net/lingluofengzang
# PythonVersion:python2.7

import sys

reload(sys)
sys.setdefaultencoding('utf-8')

lis1 = [{'OS_bit': u'64 \u4f4d', 
 'OS_version': '10.0.10240', 
 'OS_name': u'Microsoft Windows 10 \u4f01\u4e1a\u7248 2015 \u957f\u671f\u670d\u52a1\u65b9\u6848'}]
lis2 = [{'ip':'10.20.122.32'}]
lis3 = [{'CPU_name': u'Intel(R) Core(TM) i5-4200H CPU @ 2.80GHz'}]
lis4 = [{'memory_size': '1600MHz', 
 'memory_name': u'Physical Memory 0'}, 
 {'memory_size': '1600MHz', 
 'memory_name': u'Physical Memory 2'}]
lis5 = [{'GPU_name': u'NVIDIA GeForce GTX 950M', 
 'GPU_size': '2G'}, 
 {'GPU_name': u'Intel(R) HD Graphics 4600', 
 'GPU_size': '1G'}]

is_all = [lis1,lis2,lis3,lis4,lis5]
#l print lis_all

new_lis = []
for j in range(2):
 lis = {}
 for i in range(len(lis_all)):
 try:
  lis = dict(lis, **lis_all[i][j])
 except Exception as e:
  pass
 # else:
 new_lis.append(lis)

print new_lis

3.结果

以上这篇Python中使用filter过滤列表的一个小技巧分享就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持python博客。

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!