开发者

Joining split list into a list

开发者 https://www.devze.com 2023-02-26 19:03 出处:网络
I\'m trying to take a split up list and join them into another list. For 开发者_JAVA百科example, I have this 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"]

0

精彩评论

暂无评论...
验证码 换一张
取 消