I have a string which contains coordinates of chess pieces:
pieces = [Ka4Qb3Td7b4c4]
How can i split the string into a list, separated by the numbers?
Wanted output : ['Ka4', 'Qb3', 'Td7', 'b4', 'c4']
Smells like homework, but easy enough..
import re
pieces = 'Ka4Qb3Td7b4c4'
m = re.compile('[A-Za-z]{1,2}\d+')
print m.findall(pieces)
Yields..
['Ka4', 'Qb3', 'Td7', 'b4', 'c4']
This works for me:
import re
mylist = []
pieces = "Ka4Qb3Td7b4c4"
for chunk in re.finditer("(.*?[0-9]{1})",pieces):
mylist.append(chunk.group(1))
print mylist
You may need to adjust the regex if there are 2 digit seperators (I'm not a chess guy...)
For interest sake, I reworked it as the suggested list comprehension and agree it's much cleaner:
import re
pieces = "Ka4Qb3Td7b4c4"
mylist = [ chunk.group(0) for chunk in re.finditer(".*?\d+",pieces) ]
print mylist