Element wise concatenate multiple lists (list of list of strings)

∥☆過路亽.° 提交于 2020-01-24 03:45:06

问题


i have a list of list of strings as below

lst = [['a','b','c'],['@','$','#'],['1','2','3']]

I want to concatenate each string inside the lists element wise, expected output as below:

['a@1','b$2','c#3']

The size of lst can vary. is there any way to accomplish this without going through for loops.
I tried to use map, but its not working.

map(str.__add__,(x for x in list))

please help.


回答1:


Here's one way zipping the sublists and mapping with ''.join the resulting tuples:

list(map(''.join, zip(*lst)))
# ['a@1', 'b$2', 'c#3']

Here zip as shown in the docs aggregates elements from several iterables. By adding *, we are unpacking the list, which means that the function will instead be receiving zip(['a','b','c'],['@','$','#'],['1','2','3']).

Now at each iteration, map will be applying ''.join to each of the aggregated iterables, i.e to the first element in each sublist, then the second, and so on.




回答2:


Not as elegant as yatu's answer but if you're using pandas:

import pandas as pd
pd.DataFrame(lst).sum(axis=0)

# 0    a@1
# 1    b$2
# 2    c#3
# dtype: object

Pandas Series have a .tolist() method to get the expected output:

series = pd.DataFrame(lst).sum(axis=0)
series.tolist()

# ['a@1', 'b$2', 'c#3']



回答3:


I had the following issues also with strings inside the list,
1) extra white spaces including leading and trailing,
2) also had to concatenate the strings with space in between.
3) Length of list inside the list are not the same always, for the below example length of list[0],list[1] and list[2] are 4,4 and 3.

example list as below :

lst= [['  It is    raining  ','  Hello Mr.   x','  Life   is what   happens','This'],
      ['cats   and   dogs','how are you','when you are busy','ends here'],
      ['here now    ','doing today?    ','making   other   plans  ']]

First and second cases can be solved by spliting each element in the list (default split using space, which will remove all the white spaces) and then joining them together with a single space in between.
Concatenating elements of varying length can be done using itertools.zip_longest, with fillvalue='' , as below:

from itertools import zip_longest
[' '.join(x.split()) for x in map(' '.join, zip_longest(*lst,fillvalue=''))]

output as below:

['It is raining cats and dogs here now',
 'Hello Mr. x how are you doing today?',
 'Life is what happens when you are busy making other plans',
 'This ends here']


来源:https://stackoverflow.com/questions/56479890/element-wise-concatenate-multiple-lists-list-of-list-of-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!