小编cap*_*tor的帖子

在Python中查找字符串中多个字符的最后一次出现

我想在字符串中找到最后一次出现的字符数.

str.rfind()将给出字符串中单个字符最后一次出现的索引,但我需要最后一次出现多个字符的索引.例如,如果我有一个字符串:

test_string = '([2+2])-[3+4])'
Run Code Online (Sandbox Code Playgroud)

我想要一个函数,它返回{,[,或{的相似的最后一次出现的索引

test_string.rfind('(', '[', '{')
Run Code Online (Sandbox Code Playgroud)

理想情况下会返回8.最好的方法是什么?

max(test_string.rfind('('), test_string.rfind('['), test_string.rfind('{'))
Run Code Online (Sandbox Code Playgroud)

看起来很笨重而不是Pythonic.

python string

5
推荐指数
1
解决办法
1026
查看次数

标签 统计

python ×1

string ×1