使用子字符串列表进行子字符串搜索后返回列表条目

时间:2018-08-25 05:50:35

标签: python arrays string substring

这个问题从以下两个主题中略有分支:

How to test if a string contains one of the substrings in a list?

Check if a Python list item contains a string inside another string

因为它是非常紧密相关的,但它似乎并没有真正给我所需的答案。我试图对我一直在阅读的内容进行逆向工程,但没有成功。所以去了:

如果我想打印出“匹配项”列表的成员,该如何处理? :/

myObjs = ['R_obj', 'objectLeft']
sideNames = ['L_','R_', '_L', '_R', 'Right', 'Left', 'right', 'left']

for i in myObjs:
    xx = [side in i for side in sideNames]
    if any (xx):
        print "something is found in " + i

    # ================= other attempts at printing:  =================
    #   print [side in i for side in sideNames] <-- gives list of booleans.
    #   print xx
    #   print str(sideNames[side]) + "found is in " + i
    #   print sideNames.side
    #   print side
    # ================= other attempts at printing:  =================

基本上,我尝试打印sideNames 列表成员 ,但是我的不同尝试都给了我错误的语法:// >

PS:我感觉自己在错误地使用[]和()

1 个答案:

答案 0 :(得分:1)

myObjs = ['R_obj', 'objectLeft']
sideNames = ['L_','R_', '_L', '_R', 'Right', 'Left', 'right', 'left']

for i in myObjs:
    for side in sideNames:
        if side in i:
            print side, "is found in ", i

将给出结果:

R_ is found in  R_obj
Left is found in  objectLeft