【问题标题】:How to store data in a float type? (append)如何以浮点类型存储数据? (附加)
【发布时间】:2018-08-05 09:44:51
【问题描述】:

如何在浮点型(追加)Python中存储数据?

data=                    #data is a float64
 [ 335  -2.2827743]
 [ 340   4.5311280]
 [ 358   0.4698628]
 [ 367   3.8023018]
 [ 388   2.9782774]

result=[]

i in range(0,4):
    if data[i][0]<360:    
    result.append(data[i])

我想要的结果:

result=
 [ 335  -2.2827743]
 [ 340   4.5311280]
 [ 358   0.4698628]

【问题讨论】:

  • 什么是data。一份清单?
  • 数据是 float64
  • 这只是简单的错误语法。
  • 我是 Python 新手
  • 请提供minimal reproducible example。至少,尝试运行您提供的代码。

标签: python numpy append


【解决方案1】:

你可以直接比较:

代码:

result = data[data[:, 0] < 360]

测试数据

import numpy as np
data = np.array([
    [335, -2.2827743],
    [340,  4.5311280],
    [358,  0.4698628],
    [367,  3.8023018],
    [388,  2.9782774],
])

result = data[data[:, 0] < 360]
print(result)

结果:

[[ 335.          -2.2827743]
 [ 340.           4.531128 ]
 [ 358.           0.4698628]]

【讨论】:

  • 谢谢!我也可以使用间隔吗?比如,在 300 到 350 之间?
  • 我尝试了这个,但以消息结束:TypeError: ufunc 'bitwise_and' not supported for the input types, and the input could not be safe to becovered to any supported types based on the cast rule ''safe ''
  • data[(300 &lt; data[:, 0]) &amp; (data[:, 0] &lt; 350)]
  • 抱歉打扰了,现在结果是布尔值(True, True, True, False,False)
  • 感谢您的耐心等待!帮了大忙!
【解决方案2】:

我想这就是你想要的:

data=[
 [ 335,  -2.2827743],
 [ 340,   4.5311280],
 [ 358,   0.4698628],
 [ 367,   3.8023018],
 [ 388,   2.9782774]]

result=[]

for i in range(0,4):
    if data[i][0]<360:    
        result.append(data[i])

【讨论】:

    猜你喜欢
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    相关资源
    最近更新 更多