Mar-21-2018, 07:50 PM
That looks super wacky, but sure whatever lol.
I guess a list of dicts, which represents the different sequences of characters might work. As you go through the data, you only ever modify the newest sequence (index -1 is the last index). Then, once a key is found that already exists in that dict, you start a new sequence.
Something like this?
I guess a list of dicts, which represents the different sequences of characters might work. As you go through the data, you only ever modify the newest sequence (index -1 is the last index). Then, once a key is found that already exists in that dict, you start a new sequence.
Something like this?
>>> data = '''a : one ... b : two ... c : three ... d : four ... a : five ... b : six ... c : seven ... d : eight ... e : nine'''.split('\n') >>> data ['a : one', ' b : two', ' c : three', ' d : four', ' a : five', ' b : six', ' c : seven', ' d : eight', ' e : nine'] >>> sequences = [{}] >>> for item in data: ... pair = item.split(':') ... key = pair[0].strip() ... value = pair[1].strip() ... >>> for item in data: ... pair = item.split(':') ... key = pair[0].strip() ... value = pair[1].strip() ... if key in sequences[-1]: ... # this key has already been seen, so we're starting a new sequence ... sequences.append({}) ... sequences[-1][key] = value ... >>> sequences [{'d': 'four', 'b': 'two', 'a': 'one', 'c': 'three'}, {'d': 'eight', 'b': 'six', 'e': 'nine', 'a': 'five', 'c': 'seven'}] >>> import pprint >>> pprint.pprint(sequences) [{'a': 'one', 'b': 'two', 'c': 'three', 'd': 'four'}, {'a': 'five', 'b': 'six', 'c': 'seven', 'd': 'eight', 'e': 'nine'}]