Home > Blockchain >  Retrieve first set of strings starting with A-Z?
Retrieve first set of strings starting with A-Z?

Time:09-25

using python3 I need to accomplish the following.

Example string/list:

“Africa, Asia, Bolivia, Columbia, Connecticut, Delaware, Egypt, Finland, Germany, Haiti, India, London, Poland, Vietnam, zebra, Boston, California, Mississippi, Russia, vacation”

I need to retrieve the first set of words starting with A-Z. I don’t want anything after the first set.

In the example, the result would be: “ Africa, Asia, Bolivia, Columbia, Connecticut, Delaware, Egypt, Finland, Germany, Haiti, India, London, Poland, Vietnam, zebra”

Sometimes the list will be shorter starting with b or c words, but if a word starts with a character which has already been retrieved, that is where the result should stop.

I hope this is clear enough for someone to assist with. Thank you all I’m advance for your help!!

CodePudding user response:

Using itertools.takewhile:

import itertools

seq = "Africa, Asia, Bolivia, Columbia, Connecticut, Delaware, Egypt, Finland, Germany, Haiti, India, London, Poland, Vietnam, zebra, Boston, California, Mississippi, Russia, vacation".split(', ')

def pred(c):
  c = c[0].lower()
  result = (pred.previous <= c)
  pred.previous = c
  return result

pred.previous = 'a'

print(list(itertools.takewhile(pred, seq)))
# ['Africa', 'Asia', 'Bolivia', 'Columbia', 'Connecticut', 'Delaware', 'Egypt', 'Finland', 'Germany', 'Haiti', 'India', 'London', 'Poland', 'Vietnam', 'zebra']

Important note: you'll have to reset pred.previous='a' before using pred again on a new sequence.

  • Related