python - 将列表转换为单独的单独列表

标签 python numpy

我有一个像这样的 numpy.ndarray:

X = array([1., 1., 1., 1., 1., 1., 1., 2., 1., 1.])

我试图通过这样做转换成列表:

samples = X.reshape(len(X)).tolist()

这样的输出

[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0]

试图将以上内容转换成单独的列表。做了这个

new_list = [samples[i] for i in range(10)]

再次给出了这样的输出:

[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0]

我正在尝试获得这样的输出:

[1.0]
[1.0]
[1.0]
[1.0]
[1.0]
[1.0]
[1.0]
[2.0]
[1.0]
[1.0]

谁能帮我解决这个问题?

最佳答案

使用列表理解,more details

替换您的代码:

new_list = [samples[i] for i in range(10)]

new_list = [[samples[i]] for i in range(10)]

O/P:

[[1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [2.0], [1.0], [1.0]]

关于python - 将列表转换为单独的单独列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56148077/

相关文章:

Python 根据另一列中的 Nan 或 boolean 值在一列中移动值

python - Python 的垃圾收集器如何检测循环引用?

python - 如何在选择下拉列表中从 onchange 调用 url 链接

python - 为什么我的 GradientDescentOptimizer 会产生 NaN?

python - 具有非线性趋势的去趋势通量时间序列

python - 大型内存映射数组的高效点积

python - 在类中编写代码和在 Python 中 def __init__(self) 中编写代码有什么区别?

python - 使用 NumPy 对灰度图像进行直方图均衡化

python - 如何在Tkinter界面中显示来自OpenCV的图像?

python - 在 Pandas Dataframe 中运行正则表达式循环