我在列表中有以下列表
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']]
此列表每日更新。有时会有一个列表,其中第一个元素有两个句点(。),如下面的bbd.pr.c
['bbd.pr.c', '15.20', '43,737', '-7.3']
当发生这种情况时,我希望删除整个列表。不确定如何去做这件事。使用计数功能。
.count('.')<=1
有任何帮助吗?
答案 0 :(得分: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))
答案 1 :(得分:1)
我从未想过filter
与列表推导相比特别易读,特别是如果你必须使用lambda。对于两个Python版本,这相当于Smac89的答案:
results = [x for x in results if x[0].count('.') <= 1]
(x
对应l
,但l
通常不是一个好名字,因为1
,l
和I
都看起来很好类似)