条件语句和子集 - Python

时间:2017-11-23 15:45:01

标签: python if-statement conditional

我有以下数据集作为ndarray:

print(predict)
[[[  7.03453398e+00   1.00585070e+01   5.34464791e-03   4.08430994e-02
     1.73265897e-02   1.48283215e-02  -1.95482448e-02  -5.05701825e-02
     8.75583757e-03   6.01415014e+00   7.04624176e+00   8.97313499e+00
    -1.38850473e-02  -6.31546229e-02   4.99860048e+00   6.01915455e+00 ...

我想更新此套装,打印 0 如果< 0 舍入每个值。

如何在Python 3中轻松实现它。?

由于

3 个答案:

答案 0 :(得分:1)

new_pred = [int(pred) if pred > 0 else 0 for pred in np.nditer(predict)]
print(new_pred)

答案 1 :(得分:0)

既然你说它是http://192.168.1.102/test.php <? msg username hello world ?> ,我认为允许numpy。这适用于您正在寻找的东西吗?

ndarray

答案 2 :(得分:0)

你可以试试这个:

  

一线解决方案:

import numpy as np

a=np.array([[[  7.03453398e+00,   1.00585070e+01 ,  5.34464791e-03,   4.08430994e-02,
     1.73265897e-02,   1.48283215e-02,  -1.95482448e-02,  -5.05701825e-02,
     8.75583757e-03,   6.01415014e+00,   7.04624176e+00,   8.97313499e+00,
    -1.38850473e-02,  -6.31546229e-02,   4.99860048e+00,   6.01915455e+00]]])

print([0 if i<0 else int(i) for i in a[0][0] ])
  

输出:

[7, 10, 0, 0, 0, 0, 0, 0, 0, 6, 7, 8, 0, 0, 4, 6]
  

详细解决方案:

以上列表全面与:

相同
round=[]
for i in a[0][0]:
    if i<0:
        round.append(0)
    else:
        round.append(int(i))

print(round)