搜索多个关键字和相应的索引

拉胡尔·阿加瓦尔

我有这样的字符串:

a = "currency is like gbp"

a= "currency blah blah euro"

a= "currency is equivalent to usd" .....

无论我在哪里找到“ gbp”,“ euro”或“ usd”中的任何一个,我都想对上面的字符串进行子字符串化或切片。

不起作用:

i = a.find("gbp") or a.find("euro") or a.find("usd")
a = a[i:]

可以做:

x = a.find('gbp')
y = a.find('euro')
z = a.find('usd')

但是然后我需要检查它们中的哪个大于-1,然后使用该变量将字符串切成薄片,这将是太多的代码。

另外,在我的原始示例中,我有10多种货币,因此需要可扩展的解决方案。

概要:

想要对找到的所有单词中的主要句子进行切片/细分

安德鲁·麦克道威尔

您可以尝试类似:

currency_array = ['gbp', 'euro', 'usd']

index = max(a.find(currency) for currency in currency_array)

print(a[index:])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章