繁体   English   中英

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

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

我有2个要处理的清单。 第一个是nameList,它对应于我要在其中找到匹配项的名称,第二个列表是该命名列表的状态。 我希望能够浏览第一个列表并在字符串中查找匹配项,如果存在匹配项,则将第一个列表中的元素抓取到新列表中,还可以抓取第二个列表中的相应元素以获取名称和状态对。 我已经尝试了几种方法,但仍无法解决问题,并且在板上查看了各种列表理解问题,也没有找到适合我的情况的解决方案。

例如,在下面的代码中,我想获取这两个条目的“ abc-1”和“ abc-2”条目以及“ ok”和“ ok”状态,并将其输出为finalNameList和finalStatusList 。

对于任何人都可以提供的任何帮助,我将不胜感激。

在我当前的实现中,我收到类型错误:“期望的字符串或缓冲区”

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是一个整数。 因此,正则表达式正在搜索scac中以整数值定义的字符串。 即它在1搜索'abc'

创建for循环的更好方法是:

for i in nameList:

这样, 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