Учитывая это:
>>> abc = [['word1_word1.5', 'word2', 'word3', 'word4_word4.5'], ['word5_word5.5', 'word2', 'word3', 'word1_word1.5']]
Разделение каждого слова:
>>> [[w.split('_') for w in l] for l in abc]
[[['word1', 'word1.5'], ['word2'], ['word3'], ['word4', 'word4.5']], [['word5', 'word5.5'], ['word2'], ['word3'], ['word1', 'word1.5']]]
Конкатенация подсписков:
>>> from itertools import chain
>>> [list(chain.from_iterable(w.split('_') for w in l)) for l in abc]
[['word1', 'word1.5', 'word2', 'word3', 'word4', 'word4.5'], ['word5', 'word5.5', 'word2', 'word3', 'word1', 'word1.5']]
Разделение только при появлении w
в xyz
:
>>> xyz = {'word1_word1.5', 'word5_word5.5', 'word4_word4.5'}
>>> [list(chain.from_iterable(w.split('_') if w in xyz else [w] for w in l)) for l in abc]
[['word1', 'word1.5', 'word2', 'word3', 'word4', 'word4.5'], ['word5', 'word5.5', 'word2', 'word3', 'word1', 'word1.5']]