我有两个列表,一个有很多组件,另一个有组件和它们的描述。我需要找到一种方法来过滤掉所有无用的信息,同时保持描述列表的顺序与组件列表相同。
我试着使用列表理解,但这并没有给我预期的结果。
lst = []
for i in range (len(components)):
lst.append([x for x in description if components[i] in x])以下是这两个变量的简短版本;
components = ['INVALID' , 'R100' , 'R101' , 'C100' , 'R100' , 'R100']
description = [
' 30_F "30_F";',
' POWER_IN1 Supply 2 At 5 Volts, 0.8 Amps;',
' R100 OPEN PN"10057609" "RES S 5mOhm 1% 2512_H6_1(T)";',
' R101 100 5 5 f PN"66151002538" "CH-WID_ 100R -5-RR 0603 (B)";',
' C100 100n 10 10 f PN"10210616" "CFCAP X7R S 100nF 50V (T)";',
' R100 OPEN PN"10057609" "RES S 5mOhm 1% 2512_H6_1(T)";',
' R100 CLOSED PN"10057609" "RES S 5mOhm 1% 2512_H6_1 (T)" VERSION 12046547;']我期望的产出是;
' INVALID No description'
' R100 OPEN PN"10057609" "RES S 5mOhm 1% 2512_H6_1(T)";'
' R101 100 5 5 f PN"66151002538" "CH-WID_ 100R -5-RR 0603 (B)";'
' C100 100n 10 10 f PN"10210616" "CFCAP X7R S 100nF 50V (T)";'
' R100 OPEN PN"10057609" "RES S 5mOhm 1% 2512_H6_1(T)";'
' R100 CLOSED PN"10057609" "RES S 5mOhm 1% 2512_H6_1 (T)" VERSION 12046547;发布于 2019-08-15 14:14:40
通过使用str.startswith函数、辅助的seen序列和Python的for/else特性:
import pprint
... # your input data variables
seen_pos = []
res = []
for comp in components:
for i, desc in enumerate(description):
if i not in seen_pos and desc.strip().startswith(comp):
seen_pos.append(i)
res.append('{:<10}{}'.format(comp, desc.strip().replace(comp, '', 1).strip()))
break
else:
res.append('{:<10}{}'.format(comp, 'No description'))
pprint.pprint(res, width=100)产出:
['INVALID No description',
'R100 OPEN PN"10057609" "RES S 5mOhm 1% 2512_H6_1(T)";',
'R101 100 5 5 f PN"66151002538" "CH-WID_ 100R -5-RR 0603 (B)";',
'C100 100n 10 10 f PN"10210616" "CFCAP X7R S 100nF 50V (T)";',
'R100 OPEN PN"10057609" "RES S 5mOhm 1% 2512_H6_1(T)";',
'R100 CLOSED PN"10057609" "RES S 5mOhm 1% 2512_H6_1 (T)" VERSION 12046547;']发布于 2019-08-15 14:17:41
[x for x in description if x.split()[0] in components]发布于 2019-08-15 14:23:37
一种使用re的解决方案。它将维护components列表中定义的顺序:
components = ['R100' , 'R101' , 'C100' , 'R100' , 'R100']
description = [
' 30_F "30_F";',
' POWER_IN1 Supply 2 At 5 Volts, 0.8 Amps;',
' R100 OPEN PN"10057609" "RES S 5mOhm 1% 2512_H6_1(T)";',
' R101 100 5 5 f PN"66151002538" "CH-WID_ 100R -5-RR 0603 (B)";',
' C100 100n 10 10 f PN"10210616" "CFCAP X7R S 100nF 50V (T)";',
' R100 OPEN PN"10057609" "RES S 5mOhm 1% 2512_H6_1(T)";',
' R100 CLOSED PN"10057609" "RES S 5mOhm 1% 2512_H6_1 (T)" VERSION 12046547;']
import re
c = iter(components)
filtered = []
current = next(c)
for line in description:
if current and re.findall(r'^\s*{}\s*'.format(re.escape(current)), line):
filtered.append(line)
current = next(c, None)
from pprint import pprint
pprint(filtered, width=150)指纹:
[' R100 OPEN PN"10057609" "RES S 5mOhm 1% 2512_H6_1(T)";',
' R101 100 5 5 f PN"66151002538" "CH-WID_ 100R -5-RR 0603 (B)";',
' C100 100n 10 10 f PN"10210616" "CFCAP X7R S 100nF 50V (T)";',
' R100 OPEN PN"10057609" "RES S 5mOhm 1% 2512_H6_1(T)";',
' R100 CLOSED PN"10057609" "RES S 5mOhm 1% 2512_H6_1 (T)" VERSION 12046547;']https://stackoverflow.com/questions/57510950
复制相似问题