久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:2日期:2022-07-26 16:25:35

有的時候使用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過濾列表的一個小技巧分享

以上這篇Python中使用filter過濾列表的一個小技巧分享就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 长宁区| 承德市| 南京市| 定南县| 荔浦县| 新宁县| 尉氏县| 太仆寺旗| 广宁县| 连城县| 景宁| 浠水县| 磐石市| 苍梧县| 玛沁县| 杭州市| 衡东县| 阳原县| 云和县| 富平县| 阜城县| 东辽县| 邳州市| 连云港市| 泸州市| 稻城县| 双柏县| 祥云县| 宣汉县| 东兴市| 武城县| 溧水县| 综艺| 镇原县| 冷水江市| 诸城市| 焉耆| 义乌市| 沂源县| 静宁县| 泰宁县|