2016-05-01 35 views
2

我有一個列表基於Python的.Count中

results=[['hgd', '2.96', '4,433,389', '-13.7'], ['bbd.a', '2.25', '1,209,421', '-13.1'], ['mri.u', '8.60', '3,000', '-8.5'], ['iam', '1.06', '1,000', '-7.8'], ['hnd', '21.76', '1,180,466', '-7.6'], ['tth', '0.97', '41,777', '-7.6'], ['bbd.b', '1.89', '32,423,597', '-7.4'], ['bbd.pr.c', '15.20', '43,737', '-7.3'], ['esp', '1.96', '87,604', '-7.1'], ['enl', '34.00', '5,239', '-6.2'], ['rmp', '1.83', '2,688,261', '-5.7'], ['amm', '1.39', '63,301', '-5.4'], ['vrx', '41.83', '1,664,689', '-5.4'], ['xtc', '13.45', '63,453', '-5.3'], ['cxr', '36.48', '1,642,197', '-5.0']] 

內以下列表此列表每日更新從列表中刪除列表。有時,當第1個要素有兩個句號(。),如以下bbd.pr.c

['bbd.pr.c', '15.20', '43,737', '-7.3'] 

當發生這種情況我想刪除整個目錄列表。不確定如何去做這件事。使用計數功能。

.count('.')<=1 

任何幫助?

+0

是什麼。計數? – dbliss

+0

@dbliss:這是一個[Common Sequence Operation](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations) – zondo

+0

@zondo ok。這是一種在沒有關聯對象的情況下被調用的方法。 – dbliss

回答

1

對於Python 2,使用filter

results=[['hgd', '2.96', '4,433,389', '-13.7'], ['bbd.a', '2.25', '1,209,421', '-13.1'], ['mri.u', '8.60', '3,000', '-8.5'], ['iam', '1.06', '1,000', '-7.8'], ['hnd', '21.76', '1,180,466', '-7.6'], ['tth', '0.97', '41,777', '-7.6'], ['bbd.b', '1.89', '32,423,597', '-7.4'], ['bbd.pr.c', '15.20', '43,737', '-7.3'], ['esp', '1.96', '87,604', '-7.1'], ['enl', '34.00', '5,239', '-6.2'], ['rmp', '1.83', '2,688,261', '-5.7'], ['amm', '1.39', '63,301', '-5.4'], ['vrx', '41.83', '1,664,689', '-5.4'], ['xtc', '13.45', '63,453', '-5.3'], ['cxr', '36.48', '1,642,197', '-5.0']] 

results = filter(lambda l: l[0].count('.') <= 1, results) 

爲Python 3:

results = list(filter(lambda l: l[0].count('.') <= 1, results)) 
+0

謝謝,只是修復了那個 – smac89

+0

太棒了,效果很好 – ShibbyBoss

1

我從來沒有想過filter是比較列出內涵特別可讀的,特別是如果你必須使用拉姆達。這裏的Smac89的回答相當於,對於Python版本:

results = [x for x in results if x[0].count('.') <= 1] 

x相當於ll不是一般的好名字,因爲1lI看起來都太相似了)