[英]Returning a list entry after a substring search using list of substrings
This question slightly branches off of these two topics: 这个问题从以下两个主题中略有分支:
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 检查Python列表项是否在另一个字符串中包含一个字符串
Because while it's very closely related, it doesn't really seem to give me the answer i need. 因为虽然它是非常紧密相关的,但它似乎并没有给我我所需要的答案。 I've tried to reverse engineer what I've been reading to no success.
我试图对我一直在阅读的内容进行逆向工程,但没有成功。 so here goes:
所以这里去:
what If I wanted to print out the member of the "matchers" list, how do I go about it? 如果我想打印出“匹配项”列表中的成员,该怎么办? :/
:/
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: =================
Basically I'm trying to print the sideNames list member in place of "something", but my different attempts have all been giving me wrong syntaxes :/ 基本上,我试图打印sideNames 列表成员来代替“ something”,但是我的不同尝试都给了我错误的语法:/
PS: I have a feeling that im using [] and () incorrectly PS:我有一种感觉,我在错误地使用[]和()
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
will give the result: 将给出结果:
R_ is found in R_obj
Left is found in objectLeft
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.