python - Extracting specific string after specific character -


new = ['mary 2jay 3ken +', 'mary 2jay 3ken +', 'steven +john '] print(new): mary 2jay 3ken + mary 2jay 3ken + steven +john  -  

how sign/number after each person's name? i'm wondering whether dict work in case expected output is:

mary:2 jay:3 ken:+ steven:+ john:- 

to index of "+" in string, can use:

index = a_string.index("+") 

to check if "+" exist in string, use:

if "+" in a_string:     # ... 

to iterate list of string, can do:

for text in new:     # ... 

there fifty ways want. suggest read python tutorial.

edit

you can use regex extract fields name/number

for text in next:     couples = re.findall(r"(\s+)\s+(\d+|\+|\-|$)", text)     name, num in couples:         print(name, num) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -