python - Using list() on cycle object hangs system
问题描述
I'm trying to cycle through the elements of an array aa
, where the for
block is not applied over it but over another array bb
.
import numpy as np
from itertools import cycle
aa = np.array([[399., 5., 9.], [9., 35., 2.], [.6, 15., 8842.]])
c_aa = cycle(aa)
bb = np.array([33, 1., 12, 644, 234, 77, 194, 70])
for _ in bb:
print(c_aa)
This does not work, it simply outputs:
<itertools.cycle object at 0x7f8d207b1640>
<itertools.cycle object at 0x7f8d207b1640>
<itertools.cycle object at 0x7f8d207b1640>
<itertools.cycle object at 0x7f8d207b1640>
<itertools.cycle object at 0x7f8d207b1640>
<itertools.cycle object at 0x7f8d207b1640>
<itertools.cycle object at 0x7f8d207b1640>
<itertools.cycle object at 0x7f8d207b1640>
But if I change that last line for print(list(c_aa))
my entire system almost hangs.
What is going on here and how can I iterate over aa
without using it in the for
call?
解决方案
您需要实际迭代循环,例如:
for b, a_row in zip(bb, cycle(aa)):
print(b, a_row)
输出:
33.0 [399. 5. 9.]
1.0 [ 9. 35. 2.]
12.0 [6.000e-01 1.500e+01 8.842e+03]
644.0 [399. 5. 9.]
234.0 [ 9. 35. 2.]
77.0 [6.000e-01 1.500e+01 8.842e+03]
194.0 [399. 5. 9.]
70.0 [ 9. 35. 2.]
推荐阅读
- django - 生产中的 mod_wsgi 设置无法启动
- html - css父位置随子高度变化而变化
- javascript - 如何删除“输入类型文件文本字段”
- javascript - 如何合并两个没有不在主对象中的键的对象?
- rust - 将元素添加到 Rust 中的可变向量列表
- node.js - Firebase Cloud 功能问题.. 无法读取参数
- swift - 对如何在 APIClient.swift 类中创建 Stripe 临时密钥感到困惑
- pentaho - pentaho 有没有办法在选择值步骤中放置变量?
- java - 使用来自用户的输入分配给第二类中的变量
- algorithm - 重叠区间的最大子集数