简体   繁体   English

使用子字符串轻松选择查找匹配的字符串

[英]Find a matching string with substring easy selection

Is it possible to find a matching string in this boolean expression:是否可以在此布尔表达式中找到匹配的字符串:

name1 in a_string or name2 in a_string

in a more concise way to create an expression that follows such a logic:以更简洁的方式创建遵循这样逻辑的表达式:

name{1,2} in a_string:

EDIT: nameX could be a variable and it could also be a string, "nameX" (but not both, so a separate solution would be ok).编辑:nameX 可以是一个变量,也可以是一个字符串,“nameX”(但不能同时是两个,所以单独的解决方案就可以了)。

there's a way but it's looks even worse than what actual statement was the below approach is cool if you have 100+ variable and only differ in name by some thing :有一种方法,但它看起来比实际声明更糟糕,如果您有 100 多个变量并且名称仅在某些方面有所不同,则以下方法很酷:

>>> a_string = "yoyo is a toy"
>>> name1 = "yoyo"
>>> name2 = "toy"
>>> mystr = "print({}{} in a_string)"
>>> exec(compile("\n".join([mystr.format("name", i) for i in range(1,3)]), filename="<string>", mode="exec"))
True
True
>>> 

for detailed understanding read here详细了解请阅读此处

Disclaimer : above code is just for understanding purpose hope you will edit code so that it will do the same work as you want.免责声明:以上代码仅用于理解目的,希望您能编辑代码,使其按照您的意愿完成相同的工作。

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

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