[英]Overload arithmetic & comparison operators for element-wise list operations in Python
I have a class as:我有一堂课:
class Data:
def __init__(self, data: dict):
self.data = data
I want to perform operations such as我想执行诸如
d = {'a': [1, 2, 3], 'b':[4, 5, 6]}
objA = Data(d)
objA.data['a'] + 2 # this should return element-wise addition=> [3, 4, 5]
objA.data['a'] + objA.data['b'] # this should return element-wise addition=> [5, 7, 9]
objA.data['a'] > 2 # this should return element-wise comparison=> [False, False, True]
How can I overload the arithmetic and comparison operators to perform element-wise operations of lists?如何重载算术和比较运算符以执行列表的元素操作?
You can use this:你可以使用这个:
class MathList:
def __init__(self, data: list):
self.data = data
def __add__(self, n):
return [x + n for x in self.data]
def __sub__(self, n):
return [x - n for x in self.data]
def __mul__(self, n):
return [x * n for x in self.data]
def __truediv__(self, n):
return [x / n for x in self.data]
def __eq__(self, n):
return [x == n for x in self.data]
def __ne__(self, n):
return [x != n for x in self.data]
def __lt__(self, n):
return [x < n for x in self.data]
def __le__(self, n):
return [x <= n for x in self.data]
def __gt__(self, n):
return [x > n for x in self.data]
def __ge__(self, n):
return [x >= n for x in self.data]
class Data:
def __init__(self, data: dict):
self.data = {k: MathList(v) for k, v in data.items()}
d = Data({
'a': [1, 2, 3],
'b': [4, 5, 6]
})
# Arithmatic operations
print(d.data['a'] + 2) # Output: [3, 4, 5]
print(d.data['b'] - 2) # Output: [2, 3, 4]
print(d.data['a'] * 2) # Output: [2, 4, 6]
print(d.data['b'] / 2) # Output: [2.0, 2.5, 3.5]
# Boolean operations
print(d.data['a'] == 2) # Output: [False, True, False]
print(d.data['b'] != 4) # Output: [False, True, True]
print(d.data['a'] < 3) # Output: [True, True, False]
print(d.data['b'] <= 4) # Output: [True, False, False]
print(d.data['a'] > 2) # Output: [False, False, True]
print(d.data['b'] >= 5) # Output: [False, True, True]
This makes a new class called MathList
and this performs the operations you want这会创建一个名为MathList
的新类,它会执行您想要的操作
Edit: you might want to look at numpy, that can also do this编辑:你可能想看看 numpy,也可以这样做
I have defined the data
as a list
我已将data
定义为list
class Data:
def __init__(self, data: list):
self.data = data
def __add__(self, x):
self.data.append(x)
return self
def __sub__(self, x):
self.data.remove(x)
return self
def __lt__(self, x):
return [item < x for item in self.data]
def __le__(self, x):
return [item <= x for item in self.data]
def __eq__(self, x):
return [item == x for item in self.data]
def __ne__(self, x):
return [item != x for item in self.data]
def __gt__(self, x):
return [item > x for item in self.data]
def __ge__(self, x):
return [item >= x for item in self.data]
def __repr__(self):
return str(self.data)
objA = Data([1, 2, 3])
objB = Data([4, 5, 6])
objA = objA + 7
print(objA) # Output: [1, 2, 3, 7]
objA = objA - 7
print(objA) # Output: [1, 2, 3]
print(objA < 2) # Output: [True, False, False]
print(objA <= 2) # Output: [True, True, False]
print(objA == 2) # Output: [False, True, False]
print(objA != 2) # Output: [True, False, True]
print(objA < 2) # Output: [True, False, False]
print(objA >= 2) # Output: [False, True, True]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.