Python打印嵌套列表而不是列表内容的内存地址

时间:2018-10-04 19:01:25

标签: python python-3.x nested-lists

此代码(我希望按照[[7, 4, 1], [8, 5, 2], [9, 6, 3]]来打印某些内容,而是打印此内容:

[<map object at 0x7f8c1f578e10>, <map object at 0x7f8c1f578ef0>, <map object at 0x7f8c1f578fd0>]

#!/usr/bin/env python3

def transpose(matrix):
    results = [None] * len(matrix)
    for subIndex in range(len(matrix)):
        results[subIndex] = map((lambda sub : sub[subIndex]), matrix)
    return results

def rotateClockwise(matrix2):
    reversedmatrix = list(reversed(matrix2))
    rotated = transpose(reversedmatrix)
    return rotated

def main():
    m = [[1,2,3],[4,5,6],[7,8,9]]
    output = rotateClockwise(m)
    return output

if __name__ == "__main__":
    x = main()
    print(x)

这是为什么?我该如何解决?

1 个答案:

答案 0 :(得分:2)

map函数不返回列表。它返回地图对象。

您可以删除map函数以进行列表理解,这在python中是首选。

更改此行:

    results[subIndex] = map((lambda sub : sub[subIndex]), matrix)

为此:

    results[subIndex] = [sub[subIndex] for sub in matrix]