How to concatenate element-wise two lists in Python
Question:
I have two lists and I want to concatenate them element-wise. One of the list is subjected to string-formatting before concatenation.
For example :
a = [0, 1, 5, 6, 10, 11]
b = ['asp1', 'asp1', 'asp1', 'asp1', 'asp2', 'asp2']
In this case, a
is subjected to string-formatting. That is, new a
or aa
should be :
aa = [00, 01, 05, 06, 10, 11]
Final output should be :
c = ['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']
Can somebody please tell me how to do that?
Answers:
Than can be done elegantly with map and zip:
map(lambda (x,y): x+y, zip(list1, list2))
Example:
In [1]: map(lambda (x,y): x+y, zip([1,2,3,4],[4,5,6,7]))
Out[1]: [5, 7, 9, 11]
Use zip
:
>>> ["{}{:02}".format(b_, a_) for a_, b_ in zip(a, b)]
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']
not using zip. I dunno, I think this is the obvious way to do it. Maybe I just learnt C first 🙂
c=[]
for i in xrange(len(a)):
c.append("%s%02d" % (b[i],a[i]))
Using zip
[m+str(n) for m,n in zip(b,a)]
output
['asp10', 'asp11', 'asp15', 'asp16', 'asp210', 'asp211']
b = ['asp1', 'asp1', 'asp1', 'asp1', 'asp2', 'asp2']
aa = [0, 1, 5, 6, 10, 11]
new_list =[]
if len(aa) != len(b):
print 'list length mismatch'
else:
for each in range(0,len(aa)):
new_list.append(b[each] + str(aa[each]))
print new_list
Other solution (preferring printf formating style over .format()
usage), it’s also smaller:
>>> ["%s%02d" % t for t in zip(b, a)]
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']
inputs:
a = [0, 1, 5, 6, 10, 11]
b = ['asp1', 'asp1', 'asp1', 'asp1', 'asp2', 'asp2']
concat_func = lambda x,y: x + "" + str(y)
list(map(concat_func,b,a)) # list the map function
output:
['asp10', 'asp11', 'asp15', 'asp16', 'asp210', 'asp211']
If you wanted to concatenate arbitrary number of lists, you could do this:
In [1]: lists = [["a", "b", "c"], ["m", "n", "o"], ["p", "q", "r"]] # Or more
In [2]: lists
Out[2]: [['a', 'b', 'c'], ['m', 'n', 'o'], ['p', 'q', 'r']]
In [4]: list(map("".join, zip(*lists)))
Out[4]: ['amp', 'bnq', 'cor']
I ended up using a temporary DataFrame, it’s readable and fast:
a = ["a", "b", "c"]
b = ["1", "2", "3"]
df = pd.DataFrame({"a": a, "b": b})
df["c"] = df.a + df.b
result = df.c.values
Output:
$ result
["a1", "b2", "c3"]
Behind the scenes, DataFrames use numpy, so the result is efficient.
And the same thing as a function:
import pandas as pd
from typing import List
def _elementwise_concat(self, a: List[str], b: List[str]) -> List[str]:
"""
Elementwise concatenate.
:param a: List of strings.
:param b: List of strings.
:return: List, same length, strings concatenated.
"""
df = pd.DataFrame({"a": a, "b": b})
df["c"] = df.a + df.b
return df.c.values
List comprehension / zip()
/ use of zfill()
to format.
print ([y+str(x).zfill(2) for x,y in zip(a,b)])
Output:
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']
Using lambda
and format
:
b = ['asp1', 'asp1', 'asp1', 'asp1', 'asp2', 'asp2']
a = [0, 1, 5, 6, 10, 11]
list(map(lambda x: x[0] + "{0:0=2d}".format(x[1]), zip(b, a)))
Out:
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']
I have two lists and I want to concatenate them element-wise. One of the list is subjected to string-formatting before concatenation.
For example :
a = [0, 1, 5, 6, 10, 11]
b = ['asp1', 'asp1', 'asp1', 'asp1', 'asp2', 'asp2']
In this case, a
is subjected to string-formatting. That is, new a
or aa
should be :
aa = [00, 01, 05, 06, 10, 11]
Final output should be :
c = ['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']
Can somebody please tell me how to do that?
Than can be done elegantly with map and zip:
map(lambda (x,y): x+y, zip(list1, list2))
Example:
In [1]: map(lambda (x,y): x+y, zip([1,2,3,4],[4,5,6,7]))
Out[1]: [5, 7, 9, 11]
Use zip
:
>>> ["{}{:02}".format(b_, a_) for a_, b_ in zip(a, b)]
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']
not using zip. I dunno, I think this is the obvious way to do it. Maybe I just learnt C first 🙂
c=[]
for i in xrange(len(a)):
c.append("%s%02d" % (b[i],a[i]))
Using zip
[m+str(n) for m,n in zip(b,a)]
output
['asp10', 'asp11', 'asp15', 'asp16', 'asp210', 'asp211']
b = ['asp1', 'asp1', 'asp1', 'asp1', 'asp2', 'asp2']
aa = [0, 1, 5, 6, 10, 11]
new_list =[]
if len(aa) != len(b):
print 'list length mismatch'
else:
for each in range(0,len(aa)):
new_list.append(b[each] + str(aa[each]))
print new_list
Other solution (preferring printf formating style over .format()
usage), it’s also smaller:
>>> ["%s%02d" % t for t in zip(b, a)]
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']
inputs:
a = [0, 1, 5, 6, 10, 11]
b = ['asp1', 'asp1', 'asp1', 'asp1', 'asp2', 'asp2']
concat_func = lambda x,y: x + "" + str(y)
list(map(concat_func,b,a)) # list the map function
output:
['asp10', 'asp11', 'asp15', 'asp16', 'asp210', 'asp211']
If you wanted to concatenate arbitrary number of lists, you could do this:
In [1]: lists = [["a", "b", "c"], ["m", "n", "o"], ["p", "q", "r"]] # Or more
In [2]: lists
Out[2]: [['a', 'b', 'c'], ['m', 'n', 'o'], ['p', 'q', 'r']]
In [4]: list(map("".join, zip(*lists)))
Out[4]: ['amp', 'bnq', 'cor']
I ended up using a temporary DataFrame, it’s readable and fast:
a = ["a", "b", "c"]
b = ["1", "2", "3"]
df = pd.DataFrame({"a": a, "b": b})
df["c"] = df.a + df.b
result = df.c.values
Output:
$ result
["a1", "b2", "c3"]
Behind the scenes, DataFrames use numpy, so the result is efficient.
And the same thing as a function:
import pandas as pd
from typing import List
def _elementwise_concat(self, a: List[str], b: List[str]) -> List[str]:
"""
Elementwise concatenate.
:param a: List of strings.
:param b: List of strings.
:return: List, same length, strings concatenated.
"""
df = pd.DataFrame({"a": a, "b": b})
df["c"] = df.a + df.b
return df.c.values
List comprehension / zip()
/ use of zfill()
to format.
print ([y+str(x).zfill(2) for x,y in zip(a,b)])
Output:
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']
Using lambda
and format
:
b = ['asp1', 'asp1', 'asp1', 'asp1', 'asp2', 'asp2']
a = [0, 1, 5, 6, 10, 11]
list(map(lambda x: x[0] + "{0:0=2d}".format(x[1]), zip(b, a)))
Out:
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']