Created
May 7, 2020 13:44
-
-
Save StoneLabs/5d50bba80dc193b95cb4175707756eef to your computer and use it in GitHub Desktop.
Splitting school class format in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import re | |
>>> str = "10abc9df7a8z" | |
>>> searcher = re.compile("[0-9]+[a-zA-Z]+") | |
>>> blocks = searcher.findall(str) | |
>>> blocks | |
['10abc', '9df', '7a', '8z'] | |
>>> searcher = re.compile("[0-9]+|[a-zA-Z]+") | |
>>> parts = [searcher.findall(block) for block in blocks] | |
>>> parts | |
[['10', 'abc'], ['9', 'df'], ['7', 'a'], ['8', 'z']] | |
>>> classes = [[part[0] + chr for chr in part[1]] # part[0] ist die nummer | |
for part in parts] # part[1] sind die buchstaben | |
>>> classes | |
[['10a', '10b', '10c'], ['9d', '9f'], ['7a'], ['8z']] | |
>>> sum(classes, []) | |
['10a', '10b', '10c', '9d', '9f', '7a', '8z'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment