python - 如何将字符串与模式“Foo-Bar”匹配,其中 Bar 可以是列表的任何元素?
问题描述
我有一个字符串 L 的列表。
我需要检查一个字符串是直接是 L 的元素还是采用以下格式:“foo-element_of_L”
在 python 中,有没有比为 L 中的所有 X 添加“foo-X”到 L 更好的方法?
解决方案
我会做两个查找:
if x in L or f'foo-{x}' in L:
这可能比
if any(x == y or f'foo-{x}' == y for x in L):
这基本上就是你所提议的。
推荐阅读
- imagemagick-convert - 使用 GraphicsMagick MediaBox / CropBox 分辨率将 TIFF 转换为 PDF
- javascript - 无法将布尔值返回到 ionViewCanLeave
- ios - 有没有办法在 NSArray 中找到所有匹配的对象?
- javascript - 如何将所有/替换为 - 来自字符串
- python-3.x - 逻辑运算符在 Pandas 中无法正常工作
- javascript - CSS 的难点
- ios - 如何在后台禁用 Cordova webview?(仅限 iOS)
- r - 具有交叉随机效应的`nlme`
- linux - 在 Linux 中移动可用文件
- python - gnu parallel --pipe 产生空输出文件