我使用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]
综上所述,
我的代码出了什么问题?
答案 0 :(得分:0)
我不确定是不是这样,但看起来很可能你以某种方式超过了你的发电机两次。那可能吗?第二次运行生成器将产生一个空输出,请参见下面的示例。
>>> x = map(lambda _: _, range(0, 10))
>>> list(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(x)
[]