python - 从用户那里获取输入并将其附加到列表后,如何在 python 中查看列表的内容?

标签 python python-3.x list

<分区>

n = int(input())
mat = []
for i in range(n):
    row = map(int, input().split())
    mat.append(row)

print(mat)

当我运行这段代码时,我得到以下 o/p

[0x7f30e08ccba8处的 map 对象,0x7f30df3a3438处的 map 对象,0x7f30df3a3518处的 map 对象]

最佳答案

3.6中的map对象返回一个迭代器。您需要通过打印每个项目或简单地将其包装到 list( map(...., ...)) 语句中来迭代其所有值:

n = int(input())
mat = []
for i in range(n):
    row = list ( map(int, input().split()) ) # iterate all the values immediately
    mat.append(row)

print(mat)

对于 python 2.x,map 命令直接生成一个列表。

参见 https://docs.python.org/3/library/functions.html#map

关于python - 从用户那里获取输入并将其附加到列表后,如何在 python 中查看列表的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49675412/

相关文章:

python - 通过键更改其他键来更新 Python 中的字典

python3 记录器 - UnicodeEncodeError

java - 给定由 LinkedList 组成的 java hashmap 中的键,如何更新值?

python-2.7 - map(None, some_list) 对 Python 2.7 中的一个列表有什么用处吗?

python - 'coding=utf8' 和 '-*- coding: utf-8 -*-' 有什么区别?

python - 屏蔽格陵兰岛以外的岛屿和轮廓颜色(Python)

python - Python 中强制关键字参数的默认参数的用途是什么?

python - 如何在文本文件中搜索给定单词的字谜

从另一个列表中存在的两个列表中查找 2 个项目的 Pythonic 方法

python - 无法显示 Vincent 示例 map ;没有建议的解决方案有效