Python中使用filter過濾列表的一個小技巧分享
有的時候使用dir(Module),可以查看里面的方法,但是模塊自帶的屬性'__'開頭的也會顯示,如下:
>>> 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’]>>>
這個時候想過濾以'_'或'__'開頭的方法,可以:
>>> 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ù)。 其他的例子,比如想從一個列表中過濾非數(shù)字的字符串列表:
>>> L = ['1234', 'ABCD', 'BOOK']>>> filter(lambda s: s.isdigit(), L)[’1234’]>>>
補充知識:python不同長度列表,對應(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’}]
有這五個列表,要求合并成一個列表,并且所有列表的第一元素放在新列表的第一元素,以此類推。
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過濾列表的一個小技巧分享就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何基于python3和Vue實現(xiàn)AES數(shù)據(jù)加密2. 10個提供免費PHP腳本下載的網(wǎng)站3. PHP擴(kuò)展之APC——Alternative PHP Cache(可選PHP緩存)4. PHP設(shè)計模式(四)原型模式Prototype實例詳解【創(chuàng)建型】5. Java 基于UDP協(xié)議實現(xiàn)消息發(fā)送6. Python編寫nmap掃描工具7. Java向Runnable線程傳遞參數(shù)方法實例解析8. python 爬取嗶哩嗶哩up主信息和投稿視頻9. ASP.NET MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收值10. php5.6不能擴(kuò)展redis.so的解決方法
