Transpose list of lists
Python 3:
# short circuits at shortest nested list if table is jagged:
list(map(list, zip(*l)))
# discards no data if jagged and fills short nested lists with None
list(map(list, itertools.zip_longest(*l, fillvalue=None)))
Python 2:
map(list, zip(*l))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Explanation:
There are two things we need to know to understand what's going on:
- The signature of zip:
zip(*iterables)
This meanszip
expects an arbitrary number of arguments each of which must be iterable. E.g.zip([1, 2], [3, 4], [5, 6])
. - Unpacked argument lists: Given a sequence of arguments
args
,f(*args)
will callf
such that each element inargs
is a separate positional argument off
. itertools.zip_longest
does not discard any data if the number of elements of the nested lists are not the same (homogenous), and instead fills in the shorter nested lists then zips them up.
Coming back to the input from the question l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
, zip(*l)
would be equivalent to zip([1, 2, 3], [4, 5, 6], [7, 8, 9])
. The rest is just making sure the result is a list of lists instead of a list of tuples.
Transpose a Python list of lists
Use the common zip
idiom:
>>> zip(*[[1,2,3],[4,5,6],[7,8,9]])
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
Transpose a List of Lists
The following piece of code will create a list with i-th element of every list in before
:
lapply(before, "[[", i)
Now you just have to do
n <- length(before[[1]]) # assuming all lists in before have the same length
lapply(1:n, function(i) lapply(before, "[[", i))
and it should give you what you want. It's not very efficient (travels every list many times), and you can probably make it more efficient by keeping pointers to current list elements, so please decide whether this is good enough for you.
transpose a list of lists clojure
You are so close:
(defn transpose [& xs]
(apply map list xs))
Transpose nested list in python
Use zip
with *
and map
:
>>> map(list, zip(*a))
[['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH', 'III'],
['1', '262', '86', '48', '8', '39', '170', '16', '4'],
['1', '56', '84', '362', '33', '82', '296', '40', '3'],
['10', '238', '149', '205', '96', '89', '223', '65', '5'],
['92', '142', '30', '237', '336', '140', '210', '50', '2']]
Note that map
returns a map object in Python 3, so there you would need list(map(list, zip(*a)))
Using a list comprehension
with zip(*...)
, this would work as is in both Python 2 and 3.
[list(x) for x in zip(*a)]
NumPy way:
>>> import numpy as np
>>> np.array(a).T.tolist()
[['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH', 'III'],
['1', '262', '86', '48', '8', '39', '170', '16', '4'],
['1', '56', '84', '362', '33', '82', '296', '40', '3'],
['10', '238', '149', '205', '96', '89', '223', '65', '5'],
['92', '142', '30', '237', '336', '140', '210', '50', '2']]
Transposing Lists - Python
There are a couple of ways to do it.
The first one is as follows:
print(list(map(list, zip(*a))))
another option is using numpy
as follows:
print(np.array(a).T.tolist())
Another option is to use list comprehension as follows:
print([[row[i] for row in a] for i in range(len(a[0]))])
Transpose ND list of lists
It can be done numpy-less and purely functional but it's not pretty:
from itertools import starmap, repeat
a = np.ones((1,3,1234,1624)).tolist()
b = list(map(list, map(map, repeat(list), map(starmap, repeat(zip), starmap(zip, a)))))
np.shape(b)
# (1, 1234, 1624, 3)
Transpose list in groovy
Answering my own question.
After a few debugging, I realized that my list is having a single string and because of that split() was not expecting that weird input. I was able to print the first String by putting in newstr
and then split(',') code below:
all_name = proc1 | proc2
all_ids = proc3 | proc4
result_name = [all_name.text]
result_ids = [all_ids.text]
newstr = result_name[0]
result_newstr = newstr.split(',')
newids = result_ids[0]
result_newids = newids.split(',')
def aa = []
for(int i = 0;i< result_newstr.size(); i++) {
a = result_newstr[i].concat(" ").concat(result_newids[i])
aa.add(a)
}
return aa
This results:
"API (example)" "3wrhs4vp3sp5"
"Management Portal (example)" "g2828br1gzw9"
"Component1" "68pnwhltxcq0"
"Component2" "fy8g2nvvdg15"
Related Topics
How to Break Out of Multiple Loops
How to Put the Legend Outside the Plot
How to Concatenate Items in a List to a Single String
Finding the Index of an Item in a List
How to Detect Collisions Between Two Rectangular Objects or Images in Pygame
How to Unload (Reload) a Python Module
Multiprocessing VS Threading Python
Get Statistics For Each Group (Such as Count, Mean, etc) Using Pandas Groupby
Rolling or Sliding Window Iterator
Why Are Multiple Instances of Tk Discouraged
Import Multiple CSV Files into Pandas and Concatenate into One Dataframe
How to Concatenate Str and Int Objects