Python 列表的转置列表
让我们来看看:Python 列表的转置列表,python,list,transpose,Python,List,Transpose,让我们来看看: l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 我想要的结果是 r = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] 而不是 r = [(1, 4, 7), (2, 5, 8), (3, 6, 9)] 非常感谢怎么样 map(list, zip(*l)) --> [[1, 4, 7], [2, 5, 8], [3, 6, 9]] 对于Python3.x,用户可以使用 list(map(list, zip(*l)))
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
我想要的结果是
r = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
而不是
r = [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
非常感谢怎么样
map(list, zip(*l))
--> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
对于Python3.x,用户可以使用
list(map(list, zip(*l))) # short circuits at shortest nested list if table is jagged
list(map(list, itertools.zip_longest(*l, fillvalue=None))) # discards no data if jagged and fills short nested lists with None
说明:
我们需要知道两件事来了解发生了什么:
zip(*iterables)
这意味着zip
需要任意数量的参数,每个参数都必须是iterable。例如,zip([1,2],[3,4],[5,6])
args
,f(*args)
将调用f
,这样args
中的每个元素都是f
的独立位置参数itertools.zip\u longest
如果嵌套列表的元素数量不相同(同质),则不会丢弃任何数据,而是填充较短的嵌套列表,然后将其拉入拉链l=[[1,2,3],[4,5,6],[7,8,9]
,zip(*l)
相当于zip([1,2,3],[4,5,6],[7,8,9])
。剩下的只是确保结果是列表列表而不是元组列表。一种方法是使用。对于列表,a:
>>> import numpy as np
>>> np.array(a).T.tolist()
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
或者另一个没有拉链的:
>>> map(list,map(None,*a))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
相当于耶拿的解决方案:
>>> l=[[1,2,3],[4,5,6],[7,8,9]]
>>> [list(i) for i in zip(*l)]
... [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
只是为了好玩,使用有效的矩形并假设m[0]存在
>>> m = [[1,2,3],[4,5,6],[7,8,9]]
>>> [[row[i] for row in m] for i in range(len(m[0]))]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
也许不是最优雅的解决方案,但这里有一个使用嵌套while循环的解决方案:
def transpose(lst):
newlist = []
i = 0
while i < len(lst):
j = 0
colvec = []
while j < len(lst):
colvec.append(lst[j][i])
j = j + 1
newlist.append(colvec)
i = i + 1
return newlist
def转置(lst):
新列表=[]
i=0
而i
方法1和方法2在Python2或Python3中工作,并且它们在不规则的矩形列表中工作。这意味着内部列表不需要彼此具有相同的长度(参差不齐),也不需要与外部列表具有相同的长度(矩形)。其他方法很复杂
设置
方法1-
map()
,zip\u longest()
six.moves.zip_longest()
变为
- 在Python 2中
- 在Python 3中
None
。多亏了@jena,其中map()
将内部元组更改为列表。在这里,它将迭代器转换为列表。多亏了@Oregano's和@badp's
在Python3中,通过list()
传递结果以获得与方法2相同的2D列表
方法2-列表理解,
zip\u longest()
这个
方法3-
map()
的map()
-在Python 3.6中被破坏
与他的第一个代码不同,这个非常紧凑的代码可以处理不规则的2D列表,而他的第一个代码使用numpy转换并通过不规则的列表。但没有一个必须是填充值。(否,传递给内部映射()的None不是填充值。这意味着没有函数处理每个列。这些列只是传递给外部映射(),外部映射()将它们从元组转换为列表。)
在Python3中的某个地方,我不再忍受所有这些滥用:第一个参数不能是None,而且粗糙的迭代器只是被截断为最短的。其他方法仍然有效,因为这只适用于内部映射()
方法4-
map()
的map()
重新访问
唉,在Python3中,参差不齐的行不会变成参差不齐的列,它们只是被截断了。Boo-hoo progress.这里有一个解决方案,用于转换不一定是正方形的列表列表:
maxCol = len(l[0])
for row in l:
rowLength = len(row)
if rowLength > maxCol:
maxCol = rowLength
lTrans = []
for colIndex in range(maxCol):
lTrans.append([])
for row in l:
if colIndex < len(row):
lTrans[colIndex].append(row[colIndex])
maxCol=len(l[0])
对于l中的行:
rowLength=len(行)
如果rowLength>maxCol:
maxCol=行长度
lTrans=[]
对于范围内的colIndex(maxCol):
lTrans.append([])
对于l中的行:
如果colIndex
有三个选项可供选择:
1.带拉链的地图
2.列表理解
3.用于循环附加
以及查看结果:
more\u itertools.unzip()
易于阅读,并且它还可以与生成器一起使用
import more_itertools
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
r = more_itertools.unzip(l) # a tuple of generators.
r = list(map(list, r)) # a list of lists
或同等地
import more_itertools
l = more_itertools.chunked(range(1,10), 3)
r = more_itertools.unzip(l) # a tuple of generators.
r = list(map(list, r)) # a list of lists
matrix=[[1,2,3],
[1,2,3],
[1,2,3],
[1,2,3],
[1,2,3],
[1,2,3],
[1,2,3]]
行=len(矩阵)
cols=len(矩阵[0])
转置=[]
而len(转置)
方形矩阵的另一种方法。没有numpy,也没有itertools,使用(有效的)就地元素交换
def transpose(m):
for i in range(1, len(m)):
for j in range(i):
m[i][j], m[j][i] = m[j][i], m[i][j]
这就是我一直在寻找的东西,但我的脑袋一直转不过来。不过@jena的解决方案确实很短是的,它需要一些轮胎才能正确完成。好吧,尝试了很多次。仍然不是很正确——这只发生在维度为正方形时才起作用!它应该是:
[[j[i]代表l中的j]代表范围内的i(len(l[0])]]
。当然,你必须确保列表l
不是空的。@LeeD对耶拿的例子l=[[1,2],[3,4],[5]@hobs仍然不起作用,那是badp回应耶拿的例子。然而,我不确定这对我来说是否有意义。在我看来,换位意味着一个矩形矩阵——当表示为列表列表时,这意味着所有内部列表的长度必须相同。作为本例的“换位”,您希望得到什么样的结果?请注意:如果l
的大小不均匀(例如,某些行比其他行短),zip
将不会对此进行补偿,而是从输出中切掉行。因此l=[[1,2],[3,4],[5]
为您提供了[[1,3,5]]
。itertools
函数zip_longest()
可以处理不均匀列表。在回答中看到一个解释会很好:)我认为即使是list(zip(*l))
在Python3中也能正常工作。@Stefano它也能工作(在Python2中zip(*l)
),但您得到的是元组列表,而不是列表列表。美国大学
>>> map(list, map(None, *list_list))
[[1, 4, 7], [2, 5, 8], [3, 6, 9], [None, 6.1, None], [None, 6.2, None], [None, 6.3, None]]
>>> list(map(list, map(lambda *args: args, *list_list)))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]] // Python 2.7
[[1, 4, 7], [2, 5, 8], [3, 6, 9], [None, 6.1, None], [None, 6.2, None], [None, 6.3, None]] // 3.6+
maxCol = len(l[0])
for row in l:
rowLength = len(row)
if rowLength > maxCol:
maxCol = rowLength
lTrans = []
for colIndex in range(maxCol):
lTrans.append([])
for row in l:
if colIndex < len(row):
lTrans[colIndex].append(row[colIndex])
solution1 = map(list, zip(*l))
solution2 = [list(i) for i in zip(*l)]
solution3 = []
for i in zip(*l):
solution3.append((list(i)))
print(*solution1)
print(*solution2)
print(*solution3)
# [1, 4, 7], [2, 5, 8], [3, 6, 9]
import numpy as np
r = list(map(list, np.transpose(l)))
import more_itertools
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
r = more_itertools.unzip(l) # a tuple of generators.
r = list(map(list, r)) # a list of lists
import more_itertools
l = more_itertools.chunked(range(1,10), 3)
r = more_itertools.unzip(l) # a tuple of generators.
r = list(map(list, r)) # a list of lists
matrix = [[1,2,3],
[1,2,3],
[1,2,3],
[1,2,3],
[1,2,3],
[1,2,3],
[1,2,3]]
rows = len(matrix)
cols = len(matrix[0])
transposed = []
while len(transposed) < cols:
transposed.append([])
while len(transposed[-1]) < rows:
transposed[-1].append(0)
for i in range(rows):
for j in range(cols):
transposed[j][i] = matrix[i][j]
for i in transposed:
print(i)
def transpose(m):
for i in range(1, len(m)):
for j in range(i):
m[i][j], m[j][i] = m[j][i], m[i][j]