python - How to categorise components in a list of strings
问题描述
I have a list of strings which I need to go through and categorise the components. The components are in categories in the list however I'm not sure where to start.
lst = [
'CAPACITOR
C500 1u PN"1";
C501 1u PN"2";
RESISTOR
R100 10 PN"10";
R101 10 PN"11";'
,
'CAPACITOR
C500 1u PN"1";
C507 1u PN"2";
CONNECTOR
TP100 NT;
TP101 NT;']
#Above formatted as it is easier to see
lst = [ 'CAPACITOR\n C500 1u PN"1";\n C501 1u PN"2";\n\nRESISTOR\n R100 10 PN"10";\n R101 10 PN"11";\n' , '\nCAPACITOR\n C500 1u PN"1";\n C507 1u PN"2";\n\nCONNECTOR\n TP100 NT;\n TP101 NT;']
The output I expect to get is;
C500 , Capacitor
C501 , Capacitor
R100 , Resistor
R101 , Resistor
C507 , Capacitor
TP100 , Connector
TP101 , Connector
C500 only needs to be defined once.
I am able to get a list of just the components but have no idea how to get what category they are in.
解决方案
这是使用正则表达式的一种方法。
前任:
import re
lst = [ 'CAPACITOR\n C500 1u PN"1";\n C501 1u PN"2";\n\n RESISTOR\n R100 10 PN"10";\n R101 10 PN"11";\n ' , '\n CAPACITOR\n C500 1u PN"1";\n C507 1u PN"2";\n \n CONNECTOR\n TP100 NT; \n TP101 NT;']
result = []
for i in lst:
for j in i.splitlines():
if j.strip():
category = re.match(r"\s*(\b[A-Z]+\b)", j) #Check if line has category --> All Capital letters
if category:
result.append([category.group(1)])
else:
component = re.match(r"\s+(\b[A-Z0-9]+\b)", j) #Check if line has component --> Capital letter and int.
if component:
result[-1].append(component.group(1))
for i in result:
category = i[0]
for component in i[1:]:
print(component, category)
输出:
C500 CAPACITOR
C501 CAPACITOR
R100 RESISTOR
R101 RESISTOR
C500 CAPACITOR
C507 CAPACITOR
TP100 CONNECTOR
TP101 CONNECTOR
推荐阅读
- json - 通过 jq 将 aws cli 解析为 CSV
- python - 损坏的 JPEG 数据:标记 0xdb OpenCV 与 macOS Mojave 之前的 20 个无关字节
- centroid - 从几何形状字段创建质心列会产生 AttributeError: 'NoneType' object has no attribute 'centroid'
- python-3.x - 为什么子处理 tcprewrite 会导致 EOF 错误?
- sql - 在 Azure 数据工厂中成功完成数据流后,为什么没有将数据传输到我的接收器表?
- r - XLConnect 安装在 R for windows 中失败
- vba - Word VBA:将注释和用户名转换为脚注;需要作者(现在在脚注中)加粗
- ios - 如何将在构建阶段创建的文件添加为捆绑资源?
- java - queued-work-looper Handler 在主线程上发布 Runnable 时创建的线程
- r - 查找长度不等的数据帧之间的最近坐标