This is similar to splitting a list of strings into a list of lists of strings, but I want a copy of the original string as an element of the list that came from it. The purpose is I want to parse out elements from a filename, but I want to retain the filename, so after I match the list using the words, the filename is readily available, so I can do something with it.
For example,
stringList = ["wordA1_wordA2_wordA3","wordB1_wordB2_wordB3"]
becomes
splitList = [["wordA1_wordA2_wordA3","wordA1","wordA2","wordA3"],
["wordB1_wordB2_wordB3","wordB1","wordB2","wordB3"]]
I'm trying to do it in a single command as a list comprehension
The closest I've gotten is:
splitList = [[item,item.split('_')] for item in stringList]
which yields:
splitList = [["wordA1_wordA2_wordA3",["wordA1","wordA2","wordA3"]],
["wordB1_wordB2_wordB3",["wordB1","wordB2","wordB3"]]
I could work with this, but is there a more elegant suggestion that I could learn from?
I've tried
splitList = [item.split('_') + item for item in stringList]
which complains about not concatenating a list to a str.
And
splitList = [item.split('_').append(item) for item in stringList]
which creates a list of 'None's.