【问题标题】:Returning a list entry after a substring search using list of substrings在使用子字符串列表进行子字符串搜索后返回列表条目
【发布时间】:2019-01-31 12:33:26
【问题描述】:

这个问题稍微偏离了这两个主题:

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

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

如果我想打印出“matchers”列表的成员,我该怎么做? :/

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 list member 来代替“某物”,但我的不同尝试都给了我错误的语法:/

PS:我感觉我用错了 [] 和 ()

【问题讨论】:

  • 所以你想要像R_ is found in R_obj这样的输出?
  • 好的,谢谢。这正是我正在寻找的

标签: python arrays string substring


【解决方案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

【讨论】:

    猜你喜欢
    • 2014-08-30
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 2016-04-21
    • 2018-10-15
    相关资源
    最近更新 更多