- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是一个由两部分组成的 python 问题。第一部分是关于美学的,第二部分是关于实现的。底部附有我目前使用的代码示例。
我正在尝试设计一个 python 模块/类,使我能够进行惰性评估/延迟计算。设置这些计算后,我希望能够准确地确定为什么答案是这样,并修改函数的输入并重新计算输出,而无需重新定义中间变量。
本质上,我希望能够做一些尽可能接近的事情:
>>> a=3
>>> b=5
>>> c=a+b
>>> c
8
>>> a = 6
>>> c
11
和类似的东西
>>> c.calculation
'a [ 6 ] + b [ 5 ] = 11'
现在我(还)没有那么担心循环依赖,我最接近上面的是:
a = constant(2)
b = constant(3)
c = a + b
d = a * b
这给了我这样的东西:
c is 5 [ 2 <built-in function __add__> 3 ]
d is 6 [ 2 <built-in function __mul__> 3 ]
这并不完美,但已经很接近了。然后我更改 a 和 b 的值以重新计算 c、d 的值
我知道我不能使用赋值来修改 a,b 的值而不完全覆盖它,所以像 a = 2, b = 3 就出来了。
接下来,如果您查看下面的代码,您会发现我刚刚实现了 add 和 mul 的运算符,但我想重现此代码 对于(最好)所有内置运算符。
最终,我希望能够使用这个库进行一些矩阵乘积/求和。
我知道这会占用大量内存 - 但对我来说,获得“这个数字如何最终变成 x”的解释对我来说比内存使用量/CPU 周期更重要。
import operator
class operable(object):
def __add__(self,other):
if isinstance(other,operable):
d = calculation()
d.operator = operator.__add__
d.operands = [self,other]
return d
def __mul__(self,other):
if isinstance(other,operable):
d = calculation()
d.operator = operator.__mul__
d.operands = [self,other]
return d
class calculation(operable):
def __init__(self):
self.operands = []
self.operator = None
@property
def value(self):
return reduce(self.operator, [x.value for x in self.operands])
@property
def calculation(self):
return (" %s " % str(self.operator)).join([x.__repr__() for x in self.operands])
def __repr__(self):
return "%d [ %s ] " % ( self.value, self.calculation )
class constant(operable):
def __init__(self, x = 0):
self._value = x
def __repr__(self):
return "%d" %( self.value)
@property
def value(self):
return self._value
@value.setter
def value(self,new_val):
self._value = new_val
def test_1():
a = constant(2)
b = constant(3)
c = a + b
d = a * b
z = a + b + c + d
print "c is",c
print "d is",d
print "z is ",z
b.value = 5
print "c is now",c
print "d is now",d
print "z is now ",z
if __name__ == "__main__":
test_1()
最佳答案
我非常喜欢 Raymond Hettinger 解决类似问题的方法:
class SpreadSheet:
_cells = {}
tools = {}
def __setitem__(self, key, formula):
self._cells[key] = formula
def getformula(self, key):
return self._cells[key]
def __getitem__(self, key ):
return eval(self._cells[key], SpreadSheet.tools, self)
>>> from math import sin, pi
>>> SpreadSheet.tools.update(sin=sin, pi=pi, len=len)
>>> ss = SpreadSheet()
>>> ss['a1'] = '5'
>>> ss['a2'] = 'a1*6'
>>> ss['a3'] = 'a2*7'
>>> ss['a3']
210
>>> ss['b1'] = 'sin(pi/4)'
>>> ss['b1']
0.70710678118654746
>>> ss.getformula('b1')
'sin(pi/4)'
关于Python "deferred calculation"编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18784804/
我知道 Magical Record 支持聚合操作,例如 sum:、max: 但是有没有办法进行一些简单的计算,例如: 总和:属性 * other_attributes 如果我们知道这些属性的值为 N
我有一个项目可以计算一些关于用户表现的“统计数据”,然后将其展示给他们。所有这些统计数据最终都来自一个记录用户与网站交互的大型“交互”表。目前,所有这些统计数据都是通过查看这些数据来计算的。我们广泛使
我正在试着用熊猫和NumPy来计算蟒蛇中的Connors RSI。我想用ConnorsRSI的默认值(3,2,100)来计算它。。Connors RSI的公式为:[RSI(Close,3)+RSI(S
我对某种 mean() 计算有疑问。我使用带有两个标识符“ID”和“year”的面板数据集(使用 plm pkg) 我想计算变量“y”的分组平均值,但省略了第一年的计算条目,然后仅填写用于计算它的年份
我不知道这是否是微不足道的或实际上很棘手:是否可以捕获 VBA 中的“计算工作表 (shift+f9)”和“计算工作簿”事件? 我想隐藏一些操作几千行的进程,只显示一些关键值。我正在计算分布,数千行,
我和#1895500有同样的问题, 但使用 PostgreSQL 而不是 MySQL。 如何定义具有计算字段的 View ,例如: (mytable.col1 * 2) AS times_two .
如何定义具有两个计算字段的 View ,例如... ('TableName'.'BlueSquares' + 'TableName'.'RedSquares') AS TotalSquares, (
CALCULATE(m, x=red) 和 CALCULATE(m, KEEPFILTERS(x=red)) 之间有什么区别 显然它们不一样。我找到了文档和解释,但我仍然不明白。 https://le
我正在尝试从命令提示符运行我的 Java 类文件,当我尝试这样做时,我收到此错误 C:\Users\New User\workspace\myproject\bin\apackage>java cal
我正在尝试根据用户的输入显示文本。例如输入单词 APPLE 应该让它显示 BANANA。 这段代码工作正常: :Input X :If X=APPLE :Disp "BANANA" 但我不确定如何以此
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我们正在尝试实现自己的自定义购物篮计算规则集并注册新的结果 View 来获取购物篮计算结果,但我们无法找到一些如何注册新结果 View 类的信息? 我们使用这里的示例:https://support.
数字变量是否遵循 TI 计算器上的记录标准? 我真的很惊讶地注意到我的 TI 83 Premium CE 测试实际上返回了 true(即 1): 0.1 -> X 0.1 -> Y 0.01 -> Z
大约两天前,我收到了我的 TI-82 STATS 可编程计算器(实际上更像是一个 TI-83) - 并想用内置的 TI-BASIC 语言编写一个贪吃蛇游戏。 虽然我不得不找出:TI-BASIC 是 极
作为家庭作业,我们有一个基本的计算器,它只能进行+运算,我们必须实现更多的功能。我们必须实现括号运算符、符号运算符和最小最大函数。最后的任务之一是扩展最小/最大函数以计算具有两个以上参数的最小/最大,
如何从 Excel 的单元格中选择一列,然后仅计算该列?我只知道 SHIFT + F9 可以计算整个工作表,F9 可以计算整个工作簿。 谢谢你们;) 最佳答案 我认为仅使用标准 Excel 无法做到这
我已经为计算器编写了代码,但它还不能 100% 可靠地工作。每次我进行计算时,例如:“1+1=2”,并且我想要进行另一次计算,我必须关闭小程序并重新启动它。我怎样才能让它回到开始的地方。 这是代码:
意图:该程序要求用户提供其银行帐户中当前的金额、年利率和年数。输出是金额的开始和结束,显示用户指定年份的累计利息。 问题:我正在尝试找到一种正确添加利息的方法,截至目前,在指定的年份里,我所做的就是乘
我怎么让第一次点击不接受操作返回“0” 这是我的功能 $(document).ready(function(){ $('button').on('click', function(){
题目地址:https://leetcode.com/problems/basic-calculator/description/ 题目描述 Implement a basic calculator
我是一名优秀的程序员,十分优秀!