三行代码  ›  专栏  ›  技术社区  ›  shekharsabale

从列表元素捕获子字符串

  •  0
  • shekharsabale  · 技术社区  · 1 月前

        ['Communcation between: ACC_Verkehrssinn and SG_ACC_22\nUsing testing method: MinMaxMid', 
         'Communcation between: ACC_Status_Laengs and SG_ACC_22\nUsing testing method: MinMaxMid',
         'Signal Name:WLA_FC1_Obj10angleLeft\nCommuncation between: SG_WLA_FC1_Obj01_30 and 
          SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid', 
         'Signal Name:WLA_FC1_Obj10angleRight\nCommuncation between: SG_WLA_FC1_Obj01_30 and 
          SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid']
    

    WLA_FC1_Obj10angleLeft
    WLA_FC1_Obj10angleRight
    

    3 回复  |  直到 1 月前
        1
  •  0
  •   Tim Biegeleisen    1 月前

    我们可以在这里使用列表理解:

    inp = ['Communcation between: ACC_Verkehrssinn and SG_ACC_22\nUsing testing method: MinMaxMid', 'Communcation between: ACC_Status_Laengs and SG_ACC_22\nUsing testing method: MinMaxMid', 'Signal Name:WLA_FC1_Obj10angleLeft\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid', 'Signal Name:WLA_FC1_Obj10angleRight\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid']
    output = [re.search(r'Signal Name:\s*(\S+)', x).group(1) for x in inp if 'Signal Name:' in x]
    print(output)
    

    此打印:

    ['WLA_FC1_Obj10angleLeft', 'WLA_FC1_Obj10angleRight']
    
        2
  •  0
  •   NotShrirang    1 月前

    mylist = ['Communcation between: ACC_Verkehrssinn and SG_ACC_22\nUsing testing method: MinMaxMid', 
         'Communcation between: ACC_Status_Laengs and SG_ACC_22\nUsing testing method: MinMaxMid',
         'Signal Name:WLA_FC1_Obj10angleLeft\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid', 
         'Signal Name:WLA_FC1_Obj10angleRight\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid']
    
    values = []
    for mystr in mylist:
        if mystr.find("Signal Name:") != -1:
            mystr = mystr[12:]
            index = mystr.find("\n")
            mystr = mystr[:index]
            values.append(mystr)
    
    print(values)
    

    输出:

    ['WLA_FC1_Obj10angleLeft', 'WLA_FC1_Obj10angleRight']
    

    输出保存在 values

        3
  •  0
  •   DrCorgi    1 月前

    我们可以将所有字符串组合成一个大字符串,并使用re。findall()搜索单词“Signal Name:”和“\n”之间的所有单词

    希望这有帮助。

    import re
    strings = ['Communcation between: ACC_Verkehrssinn and SG_ACC_22\nUsing testing method: MinMaxMid', 
         'Communcation between: ACC_Status_Laengs and SG_ACC_22\nUsing testing method: MinMaxMid',
         'Signal Name:WLA_FC1_Obj10angleLeft\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid', 
         'Signal Name:WLA_FC1_Obj10angleRight\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid']
    all_strings = ''.join(strings)
    print(re.findall("Signal Name:(.*)\n", all_strings))
    

    输出

    ['WLA_FC1_Obj10angleLeft', 'WLA_FC1_Obj10angleRight']