我想过滤符合条件的行:
我拥有的是:
the_list = ['C TEE edBore 1 1/4200;',
'Cylinder SingleVerticalB HHJ e 1 1/8Cooling 1',
'EngineBore 11/1; TDT 8Length 3Width 3',
'EngineCy HEE Inline2008Bore 1',
'Height 4TheChallen TET e 1Stroke 1P 305',
'Height 8C ;0;Wall15ccG QBG ccGasEngineJ 142',
'Height EQE C ;0150ccGas2007',
'Length 10Wid ETQ Length 10Width ',
'Stro EHT oke 1 1/8Length ',
'Stroke 1 1/4HP JII Stroke 1 1/4HP ',
'Stroke 1Cy QTH 7Weight ; 1/2LBS',
'Weight 18LBSLength 1 DQT Length 12Width 7',
'Width 4L 233He TTE 3Height ;Weight ',
'Width ;Height 9200;Weight 4L APO .75H.P.@;5200RPM']
for a in the_list:
if '/' in a and ';' in a and 'e' in a and '@' not in a and 'k' not in a and len(a)<80:
print a
'if'语句看起来并不聪明。什么是好的方式呢?
答案 0 :(得分:5)
for line in the_list:
if all(ch in line for ch in '/;e') and \
not any(ch in line for ch in 'k@') and \
len(line) <= 80:
print line
输出结果:
C TEE edBore 1 1/4200;
EngineBore 11/1; TDT 8Length 3Width 3
答案 1 :(得分:4)
您可以使用sets;使用<=
测试严格的子集,并使用set.isdisjoint()
断言集合中没有元素存在:
if {'/', ';', 'e'} <= set(a) and {'@', 'k'}.isdisjoint(a) and len(a) <= 80:
同时注意<=
;否则80行的字符串是无效的,而您的初始条件规定只应忽略超过 80个字符的行。
演示:
>>> for a in the_list:
... if {'/', ';', 'e'} <= set(a) and {'@', 'k'}.isdisjoint(a) and len(a) <= 80:
... print a
...
C TEE edBore 1 1/4200;
EngineBore 11/1; TDT 8Length 3Width 3
答案 2 :(得分:1)
另一种解决方案是使用collections.Counter。
from collections import Counter
inc = ['/', ';', 'e']
exc = ['@', 'k']
for a in the_list:
c = Counter(a)
if all([c[k] for k in inc]) and not any([c[k] for k in exc]) and len(a)<80:
print a
输出:
C TEE edBore 1 1/4200;
EngineBore 11/1; TDT 8Length 3Width 3
但我不推荐它的可读性。我认为什么都不做很容易看到。