Python中使用filter過濾列表的一個(gè)小技巧分享
有的時(shí)候使用dir(Module),可以查看里面的方法,但是模塊自帶的屬性'__'開頭的也會(huì)顯示,如下:
>>> import random>>> dir(random)[’BPF’, ’LOG4’, ’NV_MAGICCONST’, ’RECIP_BPF’, ’Random’, ’SG_MAGICCONST’, ’SystemRandom’, ’TWOPI’, ’WichmannHill’, ’_BuiltinMethodType’, ’_MethodType’, ’__all__’, ’__builtins__’, ’__doc__’, ’__file__’, ’__name__’, ’__package__’, ’_acos’, ’_ceil’, ’_cos’, ’_e’, ’_exp’, ’_hashlib’, ’_hexlify’, ’_inst’, ’_log’, ’_pi’, ’_random’, ’_sin’, ’_sqrt’, ’_test’, ’_test_generator’, ’_urandom’, ’_warn’, ’betavariate’, ’choice’, ’division’, ’expovariate’, ’gammavariate’, ’gauss’, ’getrandbits’, ’getstate’, ’jumpahead’, ’lognormvariate’, ’normalvariate’, ’paretovariate’, ’randint’, ’random’, ’randrange’, ’sample’, ’seed’, ’setstate’, ’shuffle’, ’triangular’, ’uniform’, ’vonmisesvariate’, ’weibullvariate’]>>>
這個(gè)時(shí)候想過濾以'_'或'__'開頭的方法,可以:
>>> filter(lambda s: not s.startswith('_'), dir(random))[’BPF’, ’LOG4’, ’NV_MAGICCONST’, ’RECIP_BPF’, ’Random’, ’SG_MAGICCONST’, ’SystemRandom’, ’TWOPI’, ’WichmannHill’, ’betavariate’, ’choice’, ’division’, ’expovariate’, ’gammavariate’, ’gauss’, ’getrandbits’, ’getstate’, ’jumpahead’, ’lognormvariate’, ’normalvariate’, ’paretovariate’, ’randint’, ’random’, ’randrange’, ’sample’, ’seed’, ’setstate’, ’shuffle’, ’triangular’, ’uniform’, ’vonmisesvariate’, ’weibullvariate’]>>>
從上面來看,使用filter()函數(shù),結(jié)合lambda函數(shù)很好的完成了任務(wù)。 其他的例子,比如想從一個(gè)列表中過濾非數(shù)字的字符串列表:
>>> L = ['1234', 'ABCD', 'BOOK']>>> filter(lambda s: s.isdigit(), L)[’1234’]>>>
補(bǔ)充知識(shí):python不同長(zhǎng)度列表,對(duì)應(yīng)合并
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’}]
有這五個(gè)列表,要求合并成一個(gè)列表,并且所有列表的第一元素放在新列表的第一元素,以此類推。
2. 代碼
# !/usr/bin/env/python# _*_coding:utf-8_*_# Data:2019-04-10# Auther:蘇莫# Link:QQ2388873062# Address:https://blog.csdn.net/lingluofengzang# PythonVersion:python2.7import sysreload(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_allnew_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.結(jié)果

以上這篇Python中使用filter過濾列表的一個(gè)小技巧分享就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS 使用Sprites技術(shù)實(shí)現(xiàn)圓角效果2. 關(guān)于android連續(xù)點(diǎn)擊出現(xiàn)多個(gè)Activity界面的解決方法3. ie6,ie7,ie8完美支持position:fixed的終極解決方案4. 詳解CSS故障藝術(shù)5. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效6. 淺談Android Studio導(dǎo)出javadoc文檔操作及問題的解決7. webpack高級(jí)配置與優(yōu)化詳解8. python利用opencv如何實(shí)現(xiàn)答題卡自動(dòng)判卷9. IntelliJ IDEA調(diào)整字體大小的方法10. li中插入img圖片間有空隙的解決方案

網(wǎng)公網(wǎng)安備