使用python分析输入样例

最近做一个线性规划问题,输入样例来自于text文件,稍加思索,我认为最好的方式还是使用正则表达式来检索需要的数据。

这是测试样例

Fixed: 5 5 120

TYPE
PlasmaSample 1
GlucoseReagent 1
SerumSample 1
LactateReagent 1
UrineSample 1
PyruvateReagent 1
SalivaSample 1
GlutamateReagent 1
OptGlucose 1
OptLactate 1
OptPyruvate 1
OptGlutamate 1
Operation -1
END TYPE

NumberOfNodes: 36
Name: PlasmaSample01 PlasmaSample
NumberOfTypes: 1
0 0 2
Name: PlasmaSample02 PlasmaSample
NumberOfTypes: 1
0 0 2
Name: PlasmaSample03 PlasmaSample
NumberOfTypes: 1
0 0 2
Name: GlucoseReagent01 GlucoseReagent
NumberOfTypes: 1
0 0 2
Name: LactateReagent01 LactateReagent
NumberOfTypes: 1
0 0 2
Name: PyruvateReagent01 PyruvateReagent
NumberOfTypes: 1
0 0 2
Name: SerumSample01 SerumSample
NumberOfTypes: 1
0 0 2
Name: SerumSample02 SerumSample
NumberOfTypes: 1
0 0 2
Name: SerumSample03 SerumSample
NumberOfTypes: 1
0 0 2
Name: GlucoseReagent02 GlucoseReagent
NumberOfTypes: 1
0 0 2
Name: LactateReagent02 LactateReagent
NumberOfTypes: 1
0 0 2
Name: PyruvateReagent02 PyruvateReagent
NumberOfTypes: 1
0 0 2
Name: UrineSample01 UrineSample
NumberOfTypes: 1
0 0 2
Name: UrineSample02 UrineSample
NumberOfTypes: 1
0 0 2
Name: UrineSample03 UrineSample
NumberOfTypes: 1
0 0 2
Name: GlucoseReagent03 GlucoseReagent
NumberOfTypes: 1
0 0 2
Name: LactateReagent03 LactateReagent
NumberOfTypes: 1
0 0 2
Name: PyruvateReagent03 PyruvateReagent
NumberOfTypes: 1
0 0 2
Name: PlasmaMix01 Operation
NumberOfTypes: 4
2 2 10
1 4 5
2 4 3
2 3 6
Name: PlasmaMix02 Operation
NumberOfTypes: 4
2 2 10
1 4 5
2 4 3
2 3 6
Name: PlasmaMix03 Operation
NumberOfTypes: 4
2 2 10
1 4 5
2 4 3
2 3 6
Name: SerumMix01 Operation
NumberOfTypes: 4
2 2 6
2 4 2
1 4 3
2 3 4
Name: SerumMix02 Operation
NumberOfTypes: 4
2 2 6
2 4 2
1 4 3
2 3 4
Name: SerumMix03 Operation
NumberOfTypes: 4
2 2 6
2 4 2
1 4 3
2 3 4
Name: UrineMix01 Operation
NumberOfTypes: 4
2 2 8
1 4 4
2 3 5
2 4 3
Name: UrineMix02 Operation
NumberOfTypes: 4
2 2 8
1 4 4
2 3 5
2 4 3
Name: UrineMix03 Operation
NumberOfTypes: 4
2 2 8
1 4 4
2 3 5
2 4 3
Name: OptGlucose01 OptGlucose
NumberOfTypes: 1
1 1 10
Name: OptGlucose02 OptGlucose
NumberOfTypes: 1
1 1 10
Name: OptGlucose03 OptGlucose
NumberOfTypes: 1
1 1 10
Name: OptLactate01 OptLactate
NumberOfTypes: 1
1 1 8
Name: OptLactate02 OptLactate
NumberOfTypes: 1
1 1 8
Name: OptLactate03 OptLactate
NumberOfTypes: 1
1 1 8
Name: OptPyruvate01 OptPyruvate
NumberOfTypes: 1
1 1 12
Name: OptPyruvate02 OptPyruvate
NumberOfTypes: 1
1 1 12
Name: OptPyruvate03 OptPyruvate
NumberOfTypes: 1
1 1 12

Precedence Constraint:
PlasmaSample01 PlasmaMix01
GlucoseReagent01 PlasmaMix01
PlasmaSample02 PlasmaMix02
LactateReagent01 PlasmaMix02
PlasmaSample03 PlasmaMix03
PyruvateReagent01 PlasmaMix03
SerumSample01 SerumMix01
GlucoseReagent02 SerumMix01
SerumSample02 SerumMix02
LactateReagent02 SerumMix02
SerumSample03 SerumMix03
PyruvateReagent02 SerumMix03
UrineSample01 UrineMix01
GlucoseReagent03 UrineMix01
UrineSample02 UrineMix02
LactateReagent03 UrineMix02
UrineSample03 UrineMix03
PyruvateReagent03 UrineMix03
PlasmaMix01 OptGlucose01
PlasmaMix02 OptLactate01
PlasmaMix03 OptPyruvate01
SerumMix01 OptGlucose02
SerumMix02 OptLactate02
SerumMix03 OptPyruvate02
UrineMix01 OptGlucose03
UrineMix02 OptLactate03
UrineMix03 OptPyruvate03

代码如下
import re


def get_from_file():
    with open('virto3_3.txt') as file_object:
        contents = file_object.read()
    return contents


def parse_from_contents(contents):
    pattern = re.compile("Name: (\w+)\s\w+\nNumberOfTypes: \d\n([\d\s]+)\n", re.S)
    items = re.findall(pattern, contents)
    for item in items:
        yield {
            # item[0]: item[1].replace("\n", ";").replace(";;", "")
            'name': item[0],
            'input': item[1].replace("\n", ";").replace(";;", "")
        }


def common(contents):
    sub_string = "Precedence Constraint:"
    index = contents.find(sub_string)
    handled_contents = contents.strip()[index + len(sub_string):]
    pattern = re.compile("(\w+)\s(\w+)\n", re.S)
    items = re.findall(pattern, handled_contents)
    return items;


def parse_from_precedence(contents):
    items = common(contents)
    for item in items:
        yield {
            item[1]: item[0]
        }


def get_lates(contents):
    lates = []
    items = common(contents)
    for item in items:
        if item[1] not in lates:
            lates.append(item[1])
    return lates


def main():
    contents = get_from_file()
    for item in parse_from_contents(contents):
        input = item.get("input")
        inputs = input.split(";")
        b = 0
        c = 0
        if len(inputs) > 1:
            a = int(inputs[0].split(" ")[2])
            for i in inputs:
                iis = i.lstrip().split(" ")
                if a > int(iis[2]):
                    a = int(iis[2])
                    b = c
                c += 1
        print(item.get("name") + " ------ " + inputs[b])

    print("--------------second part-------------")
    for late in get_lates(contents):
        for item in parse_from_precedence(contents):
            if item.get(late):
                print(late + "->" + item.get(late))


if __name__ == '__main__':
    main()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值