Interesting, first we need to break it up by wall type so we can do this.
>>> from itertools import groupby
>>> wallList = ['wall_l0', 'wall_l1', 'wall_broken_l0', 'wall_broken_l1',
'wall_vwh_l0','wall_vwh_l1', 'wall_vwh_broken_l0',
'wall_vwh_broken_l1', 'wall_vpi_l0', 'wall_vpi_l1',
'wall_vpi_broken_l0', 'wall_vpi_broken_l1']
>>> list(groupby(sorted(wallList), lambda wall: wall.replace('_broken', '')[:-3]))
[('wall', <itertools._grouper object at 0x1004edc50>), ('wall_vpi', <itertools._grouper object at 0x1004edb90>), ('wall_vwh', <itertools._grouper object at 0x1004eda90>)]
great now that we have the types lets separate by those that are broken.
this is what everything looks like together.
>>> from itertools import groupby
>>> wallList = ['wall_l0', 'wall_l1', 'wall_broken_l0', 'wall_broken_l1',
'wall_vwh_l0','wall_vwh_l1', 'wall_vwh_broken_l0',
'wall_vwh_broken_l1', 'wall_vpi_l0', 'wall_vpi_l1',
'wall_vpi_broken_l0', 'wall_vpi_broken_l1']
>>> values = [[list(v) for k, v in groupby(values, lambda value: '_broken_' in value)]
... for key, values in groupby(sorted(wallList), lambda wall: wall.replace('_broken', '')[:-3])]
>>> from pprint import pprint
>>> pprint(values)
[[['wall_broken_l0', 'wall_broken_l1'], ['wall_l0', 'wall_l1']],
[['wall_vpi_broken_l0', 'wall_vpi_broken_l1'],
['wall_vpi_l0', 'wall_vpi_l1']],
[['wall_vwh_broken_l0', 'wall_vwh_broken_l1'],
['wall_vwh_l0', 'wall_vwh_l1']]]
there are surely other ways, but this seems to be concise.
Here is another way:
>>> from collections import defaultdict
>>> values = defaultdict(lambda : defaultdict(list))
>>> for wall in wallList:
... if 'broken' in wall:
... values[wall[:-3].replace('_broken', '')]['broken'].append(wall)
... else:
... values[wall[:-3]]['default'].append(wall)
...
>>> values.items()
[('wall', defaultdict(<type 'list'>, {'default': ['wall_l0', 'wall_l1'], 'broken': ['wall_broken_l0', 'wall_broken_l1']})), ('wall_vpi', defaultdict(<type 'list'>, {'default': ['wall_vpi_l0', 'wall_vpi_l1'], 'broken': ['wall_vpi_broken_l0', 'wall_vpi_broken_l1']})), ('wall_vwh', defaultdict(<type 'list'>, {'default': ['wall_vwh_l0', 'wall_vwh_l1'], 'broken': ['wall_vwh_broken_l0', 'wall_vwh_broken_l1']}))]
>>>
This second method should be faster since we are only iterating once, dictionary look ups are constant, and we can access any set of walls by name as well as state ...
>>> values['wall']['default']
['wall_l0', 'wall_l1']
>>> values['wall_vpi']['default']
['wall_vpi_l0', 'wall_vpi_l1']
>>> values['wall_vpi']['broken']
['wall_vpi_broken_l0', 'wall_vpi_broken_l1']
>>>