0

I'm looking for an efficient (maybe a native function that I don't know of) way of going through a string character by character as long as each consecutive character matches a criteria. As soon as it doesn't match the whole shebang can be terminated.

Here's the loop start:

while (char == '-' or char == '+') for char in string:

How to efficiently code this line?

3 Answers 3

2

To loop through a sequence and terminate the loop once a condition isn't met, you can use break:

for char in string:
  if char not in ('-', '+'):
    break

  do_something_with(char)

However, if you want to just collect those items matching the condition, you might be looking for itertools.takewhile:

def find_sign_prefix(s):
  sign_prefix = list(itertools.takewhile(lambda char: char in ('-', '+'), s))
  return sign_prefix

print find_sign_prefix("--+-++---3.141592+-+")
# '--+-++---'

Or specifically for examining a prefix of a string, you can use a regular expression:

def find_sign_prefix(s):
  # `[+-]*` means "a '+' or '-' character, zero or more times";
  # `re.search` only matches at the beginning of a string; 
  # group 0 is the matched substring
  return re.search([+-]*, s).group(0)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I used num += len(list(takewhile(lambda char: char in ('-', '+'), string))) How fast is takewhile? These strings can be long sometimes
If you're just counting them, there's no need to collect them into a list; instead you can do num += sum(1 for _ in takewhile(lambda char: char in ('-', '+'), string)).
Also if you're interested in efficiency there are multiple optimizations to this, e.g. if you prepare the set {'+', '-'} as a global constant the checks will be faster -- sum(1 for _ in takewhile(SIGN_CHARS.__contains__)). It doesn't matter how long the string is, only how long the prefix is, since takewhile stops as soon as the condition is broken. It's also possible that the regex approach will be faster still, again if you keep the regular expression as a global constant (PREFIX_PATTERN = re.compile("[+-]*"))
0

Iterate over each character using a while loop, and break the loop if the condition you are looking for comes up

string = '....'

for i in range(len(string)-1):

    char_i = string[i]
    char_i_1 = string[i+1]
    if not (char_i == '-' and char_i_1 == '+'):
        break

Comments

0

Are you looking for checking each character against a condition like this?

string = 'AB++-A+B-'
for char in string:
    if char == '+' or char == '-':
       # Do something
       print(char)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.