I am grouping string elements within a list by the first word and by the last word in the string. I am using groupby
from itertools
to do the grouping. The process seems to work fine for the last word, however it doesn't seem to do the same for the first one.
from itertools import groupby
model_eval_cols = ['MAD model meFuelFlowStar', 'MedAD model meFuelFlowStar', 'MAD model rpmStar', 'MedAD model rpmStar']
for k, v in groupby(model_eval_cols, key=lambda x: x.split(' ')[2]):
print(k, list(v))
The above outputs
meFuelFlowStar ['MAD model meFuelFlowStar', 'MedAD model meFuelFlowStar']
rpmStar ['MAD model rpmStar', 'MedAD model rpmStar']
However if I try to get the strings grouped by the first word:
for k, v in groupby(model_eval_cols, key=lambda x: x.split(' ')[0]):
print(k, list(v))
It doesn't seem to work
MAD ['MAD model meFuelFlowStar']
MedAD ['MedAD model meFuelFlowStar']
MAD ['MAD model rpmStar']
MedAD ['MedAD model rpmStar']
This surprises me as the keys are the same