Jan-27-2022, 09:37 PM
Of course Dead_EyE obviously knows a lot more about this than EyE, sorry I.
Sitting on the train from Shanghai, I came up with this!
I just wanted to cater for input strings which are maybe not uniform. Then I think you need re.
Still, names with non-word characters will cause trouble.
Sitting on the train from Shanghai, I came up with this!
I just wanted to cater for input strings which are maybe not uniform. Then I think you need re.
Still, names with non-word characters will cause trouble.
import re mylist1 = ['Finley : 10', 'Evie : 0', 'P1 : 0', 'P2 : 5', 'P1 : 0', 'P2 : 5', 'Finley : 15', 'Evie : 5'] mylist2 = ['Finley,10', 'Evie,0', 'P1,0', 'P2,5', 'P1,0', 'P2,5', 'Finley,15', 'Evie,5'] mylist3 = ['Fin#1ley#1,10', 'Ev@ie,0', 'P&1,0', 'P&2,5', 'P*1,0', 'P$2,5', 'Finle-y,15', 'Evi!e,5'] # as long as the name is at the start of the string and the number at the end # and name and number are separated by a non-word character, this should find them. # finds a word at the beginning of the string using ^ # stops at the first non-word character pattern1 = re.compile(r'^\w+') # matches numbers at the end of the string using $ pattern2 = re.compile(r'\d+$') # having found them, can't be too hard to sort them! for m in mylist1: print('string is', m) match1 = re.search(pattern1, m) start = match1.span()[0] stop = match1.span()[1] + 1 name = m[start:stop] print('Name is', name) match2 = re.search(pattern2, m) start = match2.span()[0] stop = match2.span()[1] + 1 score = m[start:stop] print('Score is', score, '\n\n') for m in mylist2: print('string is', m) match1 = re.search(pattern1, m) start = match1.span()[0] stop = match1.span()[1] + 1 name = m[start:stop] print('Name is', name) match2 = re.search(pattern2, m) start = match2.span()[0] stop = match2.span()[1] + 1 score = m[start:stop] print('Score is', score, '\n\n') for m in mylist3: print('string is', m) match1 = re.search(pattern1, m) start = match1.span()[0] stop = match1.span()[1] + 1 name = m[start:stop] print('Name is', name) match2 = re.search(pattern2, m) start = match2.span()[0] stop = match2.span()[1] + 1 score = m[start:stop] print('Score is', score, '\n\n')