简体   繁体   中英

Python prints memory addresses of nested lists rather than list contents

This code, which I expect to print something along the lines of [[7, 4, 1], [8, 5, 2], [9, 6, 3]] , instead prints this:

[<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)

Why is this? How can I fix it?

map function doesn't return lists. It returns map objects.

You can remove the map function for a list comprehension, preferred in python.

Change this line:

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

For this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM