Python, list of strings, rows into columns -
so, problem is, want transpose list rows columns example:
["aaa", "bbb", "ccc"] => ["abc", "abc", "abc"]
can't find efficient way it.
you can simple use of zip
, unpacking:
strs = ["aaa", "bbb", "ccc"] print zip(*strs)
output tuples, though:
[('a', 'b', 'c'), ('a', 'b', 'c'), ('a', 'b', 'c')]
for strings can use:
strs = ["aaa", "bbb", "ccc"] print map(''.join, zip(*strs)) # python 3 use: list(map(''.join, zip(*strs))) # @cesar
output list of strings:
['abc', 'abc', 'abc']
''.join
used map tuples strings.
Comments
Post a Comment