在python中使用map函数时不需要输出

2024-04-26 19:08:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python 3.6读取具有以下示例内容的数据文件

45792
50003
19154,50004
11403
7456,6932
…

通过使用这段代码,其中变量data将整个内容存储在文件中

data = []
with open(path_file, newline='') as f:
    content = csv.reader(f)
    for row in content:
        data.append(row)

执行时

print(data[4])

输出是[‘7456’, ‘6932’]。为了得到结果,我想转换成integer格式:[7456, 6932],所以我添加了以下代码

int_data = [map(int, row) for row in data]

然后,我想创建一个tensorflow常量列表

list_tensor_constant = [tf.constant(list(e)) for e in int_data]

接下来,我执行两行代码

print(int_data[4])
print(list(int_data[4]))

我得到了这个结果

<map object at 0x000001B9CB9B4470>
[]

我所期望的应该是

<map object at 0x000001B9CB9B4470>
[7456, 6932]

概括地说

enter image description here

我的代码怎么了?你知道吗


Tags: 代码in示例map内容fordataobject
1条回答
网友
1楼 · 发布于 2024-04-26 19:08:10

我不确定这是不是真的,但看起来很可能你不知何故把你的发电机翻了两遍。有可能吗?在生成器上再次运行将产生空输出,请参见下面的示例。你知道吗

>>> x = map(lambda _: _, range(0, 10))
>>> list(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(x)
[]

相关问题 更多 >