【问题标题】:the array have numbers of items, then loops for set numbers of times in python数组有项目数,然后在 python 中循环设置次数
【发布时间】:2020-12-14 22:38:05
【问题描述】:
['LED_ON', 'Dummy', 'LED_INTENSITY']

我有一个包含固定数量项目的数组,现在有 3 个,但它可能或多或少。然后我想通过循环设定的次数来运行一些东西,数组中有 3 个项目,所以循环 3 次

我如何得到我的数组

subtest=[]
with open(st,'r') as f:
    for line in f:
        if line.startswith("   subtest"):
            subtest.append(line[12:-2])
        
    f.close()
print(subtest)

这是我的文件

   functional

   subtest "LED_ON"
      wire "PSOC_HB_R"    to 204152 aux alternate
      wire "PWR_PSOC_LED" to 205152 source
      wire "LED1-OUT-1"   to 216103 detector high
      wire "SENSOR-GND"   to  21606 detector low
   end subtest

   subtest "Dummy"
      wire "PWR_PSOC_LED" to 205152 aux
      wire "PSOC_HB_R"    to 204152 source alternate
   end subtest

   subtest "LED_INTENSITY"
      wire "PSOC_HB_R" to 204152 aux alternate
      wire "PWR_PSOC_LED" to 205152 source
      wire "LED1-INTENSITY-1" to  21603 detector high
      wire "SENSOR-GND" to  21606 detector low
   end subtest

for 循环

import re
#get data from wirelist.dat 
src_map = {}
in_test = False
with open(st,'r') as f:
    for line in f:
        if (line.strip() == '') : 
            #print ('blank line')
            continue
        if (not in_test) and not re.match("\s+subtest\s+\""+info+"\"",line): # skip
            print ('not in test, skip')
            continue
        elif re.match("\s+subtest\s+\""+info+"\"",line):
            print ('found subtest start')
            in_test = True
            continue
        elif (in_test) and ("end subtest" in line):
            print ('found subtest end')
            in_test = False
            break
        elif not (in_test):
            print ('not in test')
            continue
        else: # must be good line in LED subtest
            print ('good line')
            m = re.match(r"\s+wire\s+(\"[^\"]+\")\s+to\s+(\d+)\s+(aux alternate|aux|source|source alternate|detector high|detector low).*", line)
            if not m: # not a line we are interested in
                continue
            else:
                print (m)
                term1 = m.group(1)
                term2 = m.group(2)
                term3 = m.group(3)
                if term3.startswith('aux'):
                    src_map ['a'] = term1
                elif term3 == 'source' :
                    src_map ['s'] = term1
                elif term3.endswith('high'):
                    src_map ['i'] = term1
                else:
                    src_map['l'] = term1
print(src_map)

#find the data that want to be replace
in_test = False
lines_out = []
with open(file,'r') as input_file:
    for line in input_file:
        #print (line)
        if (line.strip() == '') : 
            #print ('blank line')
            continue
        if (not in_test) and not re.match("subtest\s+\""+info+"\"",line): # skip
            print ('not in test, skip')
            lines_out.append(line)
        elif re.match("subtest\s+\""+info+"\"",line):
            print ('found subtest start')
            in_test = True
            lines_out.append(line)
        elif (in_test) and ("end subtest" in line):
            print ('found subtest end')
            in_test = False
            lines_out.append(line)
        elif not (in_test):
            print ('not in test')
            lines_out.append(line)
        else: # must be good line in LED subtest
            print ('good line')
            m = re.match(r"\s+connect\s+(\w)\s+to\s+pins\s+(\d).*", line)
            if not m: # not a line we are interested in
                lines_out.append(line)
            else:
                print (m)
                term1 = m.group(1)
                term2 = src_map[term1]
                #map to wire values using connection type
                
                new_line = f"   connect {term1} to nodes {term2}\n"
                lines_out.append(new_line)

#replace data                
with open('mynewfile.dat','a') as outfile:
    outfile.writelines(lines_out)
    outfile.close()

在我的循环中有info,我想用我的数组中的项目替换它。例如,第一个循环是 subtest[0],第二个循环是 subtest[1],依此类推。

如果您能给我一些想法,那就太好了,因为我在这里被困了 3 天仍然没有结果,而且我从未见过有人使用这种循环(好吧,我只有 开始编程和学习编程 3 周所以请帮帮我)

【问题讨论】:

  • 代码的目标是什么?循环3次是为了什么目的?你想用正则表达式替换什么?
  • sorry line.split() 是从我尝试新事物时开始删除
  • 我想要替换的东西我需要做的就是循环 3 次并将信息更改为 3 项
  • 如果你想看看我想替换什么。等等让我编辑并把它放进去,它们几乎是一样的,所以我认为把它放进去没有意义
  • sry我的大脑热油门,代码已连接

标签: python arrays python-3.x loops arraylist


【解决方案1】:

有人从reddit帮助我解决了这个问题

file=input('Enter file:')
f=file.split(".")
w=(f[0])
st=('st_' + w + '.dat')
from collections import defaultdict
src_map = defaultdict(dict)
in_test = False
subtest = None
with open(st,'r') as f:
    for line in f:
        if (line.strip() == '') : 
            #print ('blank line')
            continue
        if (not in_test) and not line.startswith("   subtest"): # skip
            print ('not in test, skip')
            continue
        elif re.match("\s+subtest\s+\"([^\"]+)\"",line):
            m = re.match("\s+subtest\s+\"([^\"]+)\"",line)
            subtest = m.group(1)
            print (f'found subtest {subtest} start')
            in_test = True
            continue
        elif (in_test) and ("end subtest" in line):
            print (f'found subtest {subtest} end')
            in_test = False
            subtest = None
            continue
        elif not (in_test):
            print ('not in test')
            continue
        else: # must be good line in LED subtest
            print ('good line')
            m = re.match(r"\s+wire\s+(\"[^\"]+\")\s+to\s+(\d+)\s+(aux alternate|aux|source|source alternate|detector high|detector low).*", line)
            if not m: # not a line we're interested in
                continue
            else:
                print (m)
                term1 = m.group(1)
                term2 = m.group(2)
                term3 = m.group(3)
                if term3.startswith('aux'):
                    src_map [subtest]['a'] = term1
                elif term3 == 'source' :
                    src_map [subtest]['s'] = term1
                elif term3.endswith('high'):
                    src_map [subtest]['i'] = term1
                else:
                    src_map [subtest]['l'] = term1
print(src_map)
       
#find the data that want to be replace
in_test = False
lines_out = []
with open(file,'r') as input_file:
    for line in input_file:
        #print (line)
        if (line.strip() == '') : 
            #print ('blank line')
            continue        
        if (not in_test) and not line.startswith('subtest'): # skip
            print ('not in test, skip')
            lines_out.append(line)
        elif re.match("subtest\s+\"([^\"]+)\"",line):
            m = re.match("subtest\s+\"([^\"]+)\"",line)
            subtest = m.group(1)
            print (f'found subtest {subtest} start')
            in_test = True
            lines_out.append(line)
        elif (in_test) and ("end subtest" in line):
            print (f'found subtest {subtest} end')
            in_test = False
            lines_out.append(line)
        elif not (in_test):
            print ('not in test')
            lines_out.append(line)
        else: # must be good line in LED subtest
            print ('good line')
            m = re.match(r"\s+connect\s+(\w)\s+to\s+pins\s+(\d).*", line)
            if not m: # not a line we're interested in
                lines_out.append(line)
            else:
                print (m)
                term1 = m.group(1)
                
                term2 = src_map[subtest][term1]
                new_line = f"   connect {term1} to nodes {term2}\n"
                lines_out.append(new_line)
                
with open('mynewfile.dat','a') as outfile:
    outfile.writelines(lines_out)
    outfile.close()

os.remove(st)
os.remove(file)
os.rename("mynewfile.dat",file)

【讨论】:

    【解决方案2】:

    您可以在循环中运行循环:

    import re
    #get data from wirelist.dat 
    src_map = {}
    in_test = False
    with open(st,'r') as f:
        for line in f:
            for st in subtest:
                if (line.strip() == '') : 
                    #print ('blank line')
                    continue
                if (not in_test) and not re.match("\s+subtest\s+\""+st+"\"",line): # skip
                    print ('not in test, skip')
                    continue
                elif re.match("\s+subtest\s+\""+st+"\"",line):
                    print ('found subtest start')
                    in_test = True
                    continue
                elif (in_test) and ("end subtest" in line):
                    print ('found subtest end')
                    in_test = False
                    break
                elif not (in_test):
                    print ('not in test')
                    continue
                else: # must be good line in LED subtest
                    print ('good line')
                    m = re.match(r"\s+wire\s+(\"[^\"]+\")\s+to\s+(\d+)\s+(aux alternate|aux|source|source alternate|detector high|detector low).*", line)
                    if not m: # not a line we are interested in
                        continue
                    else:
                        print (m)
                        term1 = m.group(1)
                        term2 = m.group(2)
                        term3 = m.group(3)
                        if term3.startswith('aux'):
                            src_map ['a'] = term1
                        elif term3 == 'source' :
                            src_map ['s'] = term1
                        elif term3.endswith('high'):
                            src_map ['i'] = term1
                        else:
                            src_map['l'] = term1
    print(src_map)
    

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 2022-01-13
      • 2020-01-30
      • 2018-12-15
      相关资源
      最近更新 更多