Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8787557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:55:15+00:00 2026-06-13T21:55:15+00:00

getting positions for set of characters in a python string set of character :

  • 0

getting positions for set of characters in a python string

set of character :

    string="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    charPositionToFind=A,D,V,Y

expected Output

    postions=[0,3,21,24]

i do it this way

 def find_all(string,char):
     return [i - 1 for i in range(len(string)) if string.startswith(char, i - 1)]

 string="ABCDEYYFGHIAAJKVLMNOPDCQRSTAAVVVUVWXYZ"
 charPositionToFind=['A','D','V','Y']
 position=[]

 for char in charPositionToFind:
    s = find_all(string,char)
    position.extend(s)
 print sorted(position)

  output:
       [0, 3, 5, 6, 11, 12, 15, 21, 27, 28, 29, 30, 31, 33, 36]

But i want the best way to do this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:55:16+00:00Added an answer on June 13, 2026 at 9:55 pm

    string.index would be nice to use, but there are two issues with it.
    1) It only finds the first occurence of the character, and
    2) It raise an error if the character is not found, necessitating a check for existence before using index().

    Looking at the problem simplistically, these are two easy approaches to the problem:

    Method 1:

    for character in the string:
        for target in charPositionToFind:
            test if character == target
    

    Method 2:

    for target in charPositionToFind:
        for character in the string:
            test if character == target
    

    Runtime wise, the two methods have the same worst case of O(N x M), where N is the size of string and M is the size of charPositionToFind. However, using method 1 allows you to remove the inner loop by using a set. It also avoids having to do a sort at the end, since you are iterating through the characters of string in order. So, using list comprehension to avoid for loops:

    string = "ABCDEYYFGHIAAJKVLMNOPDCQRSTAAVVVUVWXYZ"
    charPositionToFind = 'ADVY'
    target_set = set(charPositionToFind)
    position = [index for index, char in enumerate(string) if char in target_set]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to replace a set of characters inside another string in Python? Here is
I'm getting this error: ORA-06502: PL/SQL: numeric or value error: character to number conversion
Is there is any way to set pixels on cameraPreview, like getting pixel or
i am getting the JSON response like this : [ {IndexID:1,IndexName:Consumer Confidence Index(CCI),IndexValue:34.79,Month:10,PercentageChanged:0,Position:0,Year:2011}, {IndexID:1,IndexName:Consumer
Getting message expected identifier in red for below these two statements } else if
OK have just started getting this error and I'm not sure why. I have
I am getting a HTML string as a result of querying a remote XML
I found this simple jquery plugin from jQuery Set Cursor Position in Text Area
I'm having a problem with getting the position of a div after rotate filter
Im getting a -1 value when i try to get the selected item position

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.