find last occurence of multiple characters in a string in Python

capt-calculator

I would like to find the last occurrence of a number of characters in a string.

str.rfind() will give the index of the last occurrence of a single character in a string, but I need the index of the last occurrence of any of a number of characters. For example if I had a string:

test_string = '([2+2])-[3+4])'

I would want a function that returns the index of the last occurence of {, [, or { similar to

test_string.rfind('(', '[', '{')

Which would ideally return 8. What is the best way to do this?

max(test_string.rfind('('), test_string.rfind('['), test_string.rfind('{'))

seems clunky and not Pythonic.

michaelpri

You can use generator expression to do this in a Pythonic way.

max(test_string.rfind(i) for i in "([{")

This iterates through the list/tuple of characters that you want to check and uses rfind() on them, groups those values together, and then returns the maximum value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find last occurence of string in multiple files

From Dev

Python find last occurence in a file

From Dev

Python find last occurence in a file

From Dev

find the last occurence of characters using javascript

From Dev

PHP: Find last occurence of string in array

From Dev

php find multiple occurence of string in string

From Dev

Python: Using regex to find the last pair of occurence

From Dev

Python: Using regex to find the last pair of occurence

From Dev

Finding last occurence of string

From Dev

Finding last occurence of string

From Dev

Occurence of a multiple symbol in a string-Python

From Dev

Find last occurence of a string using findstr on windows command line

From Dev

RegEx to match last occurence of a string

From Dev

Using Regex_substr in Oracle to select string up to the last occurence of a space within \n characters length

From Dev

How do you find the last occurence of a pattern using string.find?

From Dev

sql server find out first occurence and last occurence and replace them

From Dev

Find last word of a string that has special characters

From Dev

Find Index of Last Occurence in Multidimensional Array

From Dev

ColdFusion - Last Occurence of a string using RefindNoCase

From Dev

Shorten C string from last occurence of a char?

From Dev

search for the last occurence of a string in a file and append to it

From Dev

Split string at last (or fourth) occurence of "." delimiter

From Dev

Delete the last occurence of a string in a file in unix/linux

From Dev

Extract the last occurence of a string that changes dynamically

From Dev

Split string at last (or fourth) occurence of "." delimiter

From Dev

Finding the second last occurence of a string in Regex

From Dev

Python extract occurence of a string with regex

From Dev

Counting occurence within a string Python

From Dev

Translating characters in a string to multiple characters using Python

Related Related

  1. 1

    Find last occurence of string in multiple files

  2. 2

    Python find last occurence in a file

  3. 3

    Python find last occurence in a file

  4. 4

    find the last occurence of characters using javascript

  5. 5

    PHP: Find last occurence of string in array

  6. 6

    php find multiple occurence of string in string

  7. 7

    Python: Using regex to find the last pair of occurence

  8. 8

    Python: Using regex to find the last pair of occurence

  9. 9

    Finding last occurence of string

  10. 10

    Finding last occurence of string

  11. 11

    Occurence of a multiple symbol in a string-Python

  12. 12

    Find last occurence of a string using findstr on windows command line

  13. 13

    RegEx to match last occurence of a string

  14. 14

    Using Regex_substr in Oracle to select string up to the last occurence of a space within \n characters length

  15. 15

    How do you find the last occurence of a pattern using string.find?

  16. 16

    sql server find out first occurence and last occurence and replace them

  17. 17

    Find last word of a string that has special characters

  18. 18

    Find Index of Last Occurence in Multidimensional Array

  19. 19

    ColdFusion - Last Occurence of a string using RefindNoCase

  20. 20

    Shorten C string from last occurence of a char?

  21. 21

    search for the last occurence of a string in a file and append to it

  22. 22

    Split string at last (or fourth) occurence of "." delimiter

  23. 23

    Delete the last occurence of a string in a file in unix/linux

  24. 24

    Extract the last occurence of a string that changes dynamically

  25. 25

    Split string at last (or fourth) occurence of "." delimiter

  26. 26

    Finding the second last occurence of a string in Regex

  27. 27

    Python extract occurence of a string with regex

  28. 28

    Counting occurence within a string Python

  29. 29

    Translating characters in a string to multiple characters using Python

HotTag

Archive