简体   繁体   English

在具有2个列表的字符串列表中搜索子字符串匹配项

[英]Searching for a substring match in a string list with 2 lists

I have 2 lists that I'm trying to work with. 我有2个要处理的清单。 The first is a nameList that corresponds to names that I want to find a match within and the second list is a status of that named list. 第一个是nameList,它对应于我要在其中找到匹配项的名称,第二个列表是该命名列表的状态。 I want to be able to look through the 1st list and look for a match within the string and if there is a match grab the element in the first list into a new list and also grab the corresponding element in the second list to get a name and status pair. 我希望能够浏览第一个列表并在字符串中查找匹配项,如果存在匹配项,则将第一个列表中的元素抓取到新列表中,还可以抓取第二个列表中的相应元素以获取名称和状态对。 I've tried several approaches at this and have not been able to get it right and have looked at various list comprehension questions on the board and haven't been able to find a solution that works for my case. 我已经尝试了几种方法,但仍无法解决问题,并且在板上查看了各种列表理解问题,也没有找到适合我的情况的解决方案。

For example in the code below, I would like to grab the 'abc-1' and 'abc-2' entries as well as the 'ok' and 'ok' status for both of those entries and output those as the finalNameList and finalStatusList. 例如,在下面的代码中,我想获取这两个条目的“ abc-1”和“ abc-2”条目以及“ ok”和“ ok”状态,并将其输出为finalNameList和finalStatusList 。

I'd be grateful for any help anyone could provide. 对于任何人都可以提供的任何帮助,我将不胜感激。

In my current implementation I am getting a type error : 'expected string or buffer' 在我当前的实现中,我收到类型错误:“期望的字符串或缓冲区”

import re
import os
import sys
import getopt
import pdb

nameList = ['abc-1', 'abc-2', 'def-1', 'def-2']
statusList = ['ok', 'ok', 'bad', 'bad']
scac = 'abc'


def scacFilter (scac, nameList, statusList):
    if not scac:
        newNameList = nameList
        newStatusList = statusList
    else:
        for i in nameList:
            if re.search(scac, i):
                name = nameList[i]
                status = statusList[i]
                newNameList.append(name)
                newStatusList.append(status)
            else:
                print 'no scac match'
    return newNameList, newStatusList


finalNameList, finalStatusList = scacFilter(scac, nameList, statusList)

i is an integer. i是一个整数。 So the regular expression is searching for the string defined in scac in an integer value. 因此,正则表达式正在搜索scac中以整数值定义的字符串。 ie it's searching for 'abc' in 1 . 即它在1搜索'abc'

A better way to create your for loop would be: 创建for循环的更好方法是:

for i in nameList:

This way, i is actually the string in nameList (ie 'abc-1' , 'abc-2' , etc...) and not an integer, thus you'll be performing your regex on the string you intend to! 这样, i实际上是nameList的字符串(即'abc-1''abc-2'等),而不是整数,因此,您将对打算使用的字符串执行正则表达式!

 import os nameList = ['abc-1', 'abc-2', 'def-1', 'def-2'] statusList = ['ok', 'ok', 'bad', 'bad'] scac = 'abc' def scacFilter (scac, nameList, statusList): resultList = [] resultVal = [] for val in nameList: if not val.find(scac): indexVal = nameList.index(val) resultList.append(nameList[indexVal]) resultVal.append(statusList[indexVal]) return resultList, resultVal finalNameList, finalStatusList = scacFilter(scac, nameList, statusList) print finalNameList print finalStatusList 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM