Python中是否有可用于实现逻辑数据削减的语言?

时间:2014-10-24 14:19:43

标签: python logic

我们说我有以下形式的数据:

variable1 = [23, 20, 13, 24, 25, 28, 26, 17, 18, 29]
variable2 = [37, 46, 41, 40, 40, 39, 43, 42, 45, 56]
variable3 = [101, 120, 110, 140]

现在让我们说我想应用一些数据削减。也许我可以满足以下要求:

  • 我要求variable1的所有值都大于或等于20.
  • 我要求variable2的所有值都大于或等于40且小于或等于45.
  • 如果variable1的某些值已通过剪切,则variable3未应用剪切。
  • 如果variable1的某些值未通过剪切,则我要求variable3的所有值都大于115。

是否有一些语言可以用于Python来紧凑地描述这些逻辑数据切割,然后可以应用?我可以设想一些类似下面的内容:

data = DataClass(variables = [variable1, variable2, variable3])
data.applyCut(cut = "variable1 >=20; 40 <= variable2 <= 45")
data.applyCut(cut = "if no variable1 then variable3 > 115")
data.print()

我知道这种切割逻辑可以通过以下方式实现:

>>> variable1 = [23, 20, 13, 24, 25, 28, 26, 17, 18, 29]
>>> variable2 = [37, 46, 41, 40, 40, 39, 43, 42, 45, 56]
>>> variable3 = [101, 120, 110, 140]
>>> variable1 = [value for value in variable1 if value >= 20]
>>> variable2 = [value for value in variable2 if (value >= 40) and (value <=45)]
>>> print(variable1)
[23, 20, 24, 25, 28, 26, 29]
>>> print(variable2)
[41, 40, 40, 43, 42, 45]
>>> if not variable1:
...     variable3 = [value for value in variable3 if value > 115]
... else:
...     variable3 = [value for value in variable3]
... 
>>> print(variable3)
[101, 120, 110, 140]

我正在寻找的是用于执行此操作和类似类型的数据操作的Python中的紧凑语言,可能使用剪切规范的字符串和特殊数据类的方法来应用剪切。

1 个答案:

答案 0 :(得分:1)

我认为numpy可能是你想要的:

import numpy as np
v1 = np.array([23, 20, 13, 24, 25, 28, 26, 17, 18, 29])
v2 = np.array([37, 46, 41, 40, 40, 39, 43, 42, 45, 56])
v3 = np.array([101, 120, 110, 140])

def myfilter(v1, v2, v3): # how I would parse your "DataClass" code
    valid_ixs = (v1 >= 20) & (v2 >= 40) & (v2 <= 45)
    if any(valid_ixs): # could also use valid_ixs.sum()
        return v3[v3 > 115]
    return v3
print(myfilter(v1, v2, v3))

# what you're doing in python
v1 = v1[v1 >= 20]
v2 = v2[(v2 >= 40) & (v2 <= 45)]
v3 = v3[v3 > 115] if v1 else v3

请注意,在python中,5 <= x <= 10会转换为5 <= x and x <= 10,对于numpy,您需要&而不是and(因为运算符重载规则,如果您& #39;好奇的。)