Joining split list into a list
I'm trying to take a split up list and join them into another list. For 开发者_JAVA百科example, I have this list:
['T', 'e', 's', 't', '\n', 'List', '\n']Now I want to join these so it looks like
['Test', 'List']How can I do this?
I'm afraid that your question is a little underspecified, as S. Lott comments, but it looks as if you just want to join all the strings together and then split where there are newlines - the following works for your example, and could be easily modified for other requirements:
>>>> ''.join(['T', 'e', 's', 't', '\n', 'List', '\n']).splitlines()
['Test', 'List']
string joining is an amazing thing
l = ['T', 'e', 's', 't', '\n', 'List', '\n']
"".join(l).split('\n')
Works by taking a "" string, creating a larger string by appending all of l to it giving "Test\nList\n". Then splitting on end of line giving ["Test", "List"]
精彩评论