so I have this list in python,
所以我在python中有这个列表,
a= [[1,2,3,4],
[2,4,5,1],
[3,4,6,2],
[2,3,4,5]]
and want to turn the list reading horizontally to vertically.
并希望将列表水平翻转为垂直。
b= [[1,2,3,2],
[2,4,4,3],
[3,5,6,4],
[4,1,2,5]]
what is the best way to do it, and most efficient way to do it? i'm pretty new to programming, sorry for being noob. thanks.
什么是最好的方法,最有效的方法呢?我很喜欢编程,对不起是菜鸟。谢谢。
4 个解决方案
#1
10
You can do it like that:
你可以这样做:
zip(*your_list)
Proof:
证明:
>>> a = [[1, 2, 3, 4], [2, 4, 5, 1], [3, 4, 6, 2], [2, 3, 4, 5]]
>>> zip(*a)
[(1, 2, 3, 2), (2, 4, 4, 3), (3, 5, 6, 4), (4, 1, 2, 5)]
#2
8
Check out numpy library. You can put your list into an array and transpose it like this:
看看numpy图书馆。您可以将列表放入数组并将其转置为:
a = array ([[1,2,3,4],
[2,4,5,1],
[3,4,6,2],
[2,3,4,5]])
a.transpose()
P.S.: Explanation of Tadeck's solution is very easy. zip
has the following signature:
P.S。:解释Tadeck的解决方案非常简单。 zip具有以下签名:
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
zip(seq1 [,seq2 [...]]) - > [(seq1 [0],seq2 [0] ...),(...)]
So, it takes a number of sequences (we don't know how much exactly) and then builds tuples in the following order: takes first element of every sequence ant puts them in the tuple, then takes second element of every sequence and puts them in the second tuple and so on. It returns list of all tuples it build during its execution.
因此,它需要许多序列(我们不知道究竟多少)然后按以下顺序构建元组:获取每个序列的第一个元素ant将它们放入元组中,然后获取每个序列的第二个元素并将它们放入在第二个元组中等等。它返回它在执行期间构建的所有元组的列表。
*lst
- is, in fact, unpacking of arguments list. You can read more about it in the following note.
* lst - 实际上是解包参数列表。您可以在以下注释中阅读更多相关信息。
I hope, now everyone understands how this pretty piece of code works. :)
我希望,现在每个人都明白这段漂亮的代码是如何工作的。 :)
#3
5
You asked about efficiency. You can use timeit for that.
你问过效率问题。你可以使用timeit。
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]" "zip(*a)"
1000000 loops, best of 3: 0.569 usec per loop
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]" "map(None, *a)"
1000000 loops, best of 3: 0.644 usec per loop
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]" "[[row[i] for row in a] for i in xrange(len(a[0]))]"
1000000 loops, best of 3: 1.43 usec per loop
>python -m timeit -s "from numpy import array; a = array([[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]])" "a.transpose()"
1000000 loops, best of 3: 0.249 usec per loop
For a large data set of [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]*1000000
对于[[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]] * 1000000的大数据集
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]*1000000" "zip(*a)"
10 loops, best of 3: 400 msec per loop
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]*1000000" "map(None, *a)"
10 loops, best of 3: 458 msec per loop
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]*1000000" "[[row[i] for row in a] for i in xrange(len(a[0]))]"
10 loops, best of 3: 770 msec per loop
>python -m timeit -s "from numpy import array; a = array([[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]*1000000)" "a.transpose()"
1000000 loops, best of 3: 0.251 usec per loop
If your lists are of different lengths, zip
truncates to the shortest length. You can use 'map' or itertools.izip_longest
to instead fill the missing values with None
.
如果您的列表具有不同的长度,则zip截断为最短的长度。您可以使用'map'或itertools.izip_longest来填充缺少的值。
#4
1
Another way is:
另一种方式是:
a= [[1,2,3,4],
[2,4,5,1],
[3,4,6,2],
[2,3,4,5]]
a = [[row[i] for row in a] for i in range(len(a[0]))]
#1
10
You can do it like that:
你可以这样做:
zip(*your_list)
Proof:
证明:
>>> a = [[1, 2, 3, 4], [2, 4, 5, 1], [3, 4, 6, 2], [2, 3, 4, 5]]
>>> zip(*a)
[(1, 2, 3, 2), (2, 4, 4, 3), (3, 5, 6, 4), (4, 1, 2, 5)]
#2
8
Check out numpy library. You can put your list into an array and transpose it like this:
看看numpy图书馆。您可以将列表放入数组并将其转置为:
a = array ([[1,2,3,4],
[2,4,5,1],
[3,4,6,2],
[2,3,4,5]])
a.transpose()
P.S.: Explanation of Tadeck's solution is very easy. zip
has the following signature:
P.S。:解释Tadeck的解决方案非常简单。 zip具有以下签名:
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
zip(seq1 [,seq2 [...]]) - > [(seq1 [0],seq2 [0] ...),(...)]
So, it takes a number of sequences (we don't know how much exactly) and then builds tuples in the following order: takes first element of every sequence ant puts them in the tuple, then takes second element of every sequence and puts them in the second tuple and so on. It returns list of all tuples it build during its execution.
因此,它需要许多序列(我们不知道究竟多少)然后按以下顺序构建元组:获取每个序列的第一个元素ant将它们放入元组中,然后获取每个序列的第二个元素并将它们放入在第二个元组中等等。它返回它在执行期间构建的所有元组的列表。
*lst
- is, in fact, unpacking of arguments list. You can read more about it in the following note.
* lst - 实际上是解包参数列表。您可以在以下注释中阅读更多相关信息。
I hope, now everyone understands how this pretty piece of code works. :)
我希望,现在每个人都明白这段漂亮的代码是如何工作的。 :)
#3
5
You asked about efficiency. You can use timeit for that.
你问过效率问题。你可以使用timeit。
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]" "zip(*a)"
1000000 loops, best of 3: 0.569 usec per loop
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]" "map(None, *a)"
1000000 loops, best of 3: 0.644 usec per loop
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]" "[[row[i] for row in a] for i in xrange(len(a[0]))]"
1000000 loops, best of 3: 1.43 usec per loop
>python -m timeit -s "from numpy import array; a = array([[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]])" "a.transpose()"
1000000 loops, best of 3: 0.249 usec per loop
For a large data set of [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]*1000000
对于[[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]] * 1000000的大数据集
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]*1000000" "zip(*a)"
10 loops, best of 3: 400 msec per loop
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]*1000000" "map(None, *a)"
10 loops, best of 3: 458 msec per loop
>python -m timeit -s "a = [[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]*1000000" "[[row[i] for row in a] for i in xrange(len(a[0]))]"
10 loops, best of 3: 770 msec per loop
>python -m timeit -s "from numpy import array; a = array([[1,2,3,4],[2,4,5,1],[3,4,6,2],[2,3,4,5]]*1000000)" "a.transpose()"
1000000 loops, best of 3: 0.251 usec per loop
If your lists are of different lengths, zip
truncates to the shortest length. You can use 'map' or itertools.izip_longest
to instead fill the missing values with None
.
如果您的列表具有不同的长度,则zip截断为最短的长度。您可以使用'map'或itertools.izip_longest来填充缺少的值。
#4
1
Another way is:
另一种方式是:
a= [[1,2,3,4],
[2,4,5,1],
[3,4,6,2],
[2,3,4,5]]
a = [[row[i] for row in a] for i in range(len(a[0]))]