歡迎您光臨本站 註冊首頁

Python中使用filter過濾列表的一個小技巧分享

←手機掃碼閱讀     bom485332 @ 2020-05-02 , reply:0

有的時候使用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 u4f01u4e1au7248 2015 u957fu671fu670du52a1u65b9u6848'}] 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 u4f01u4e1au7248 2015 u957fu671fu670du52a1u65b9u6848'}] 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.結果


[bom485332 ] Python中使用filter過濾列表的一個小技巧分享已經有257次圍觀

http://coctec.com/docs/python/shhow-post-232540.html