计算后总值不变(total value is not changing after calculation)
我这里有一个jsfiddle。
在小提琴中有许多标记文本框。 在文本框中键入一个数字,然后单击添加问题按钮。 您将看到剩余的总分数等于
10
但不会改变。 剩余标记的数量应通过减去附加行中的每个数字来改变。例如,如果剩余的总标记为
10
并且您追加四行,每行包含标记1
,则剩余的总标记应为6
。 (10 - 4 = 6
)但为什么不改变剩余的标记数量?这是执行计算的函数:
function calculateTotal() { var totalweight = totalmarks; $("#qandatbl td.weight input").each(function (i, elm){ totalweight = totalweight - Number($(elm).val(), 10); }); $("#total-weight").text(totalweight); }
I have a jsfiddle here.
In the fiddle there is a number of mark textboxes. Type a number in the text box and keep clicking on the add question button. You will see that the total marks remaining equals
10
but it doesn't change. The number of marks remaining should change by subtracting each number in the appended row.For example, if total marks remaining is
10
and you append four rows, each row containing marks of1
, then total marks remaining should be6
. (10 - 4 = 6
.) But why is it not changing the number of marks remaining?This is the function where it performs the calculation:
function calculateTotal() { var totalweight = totalmarks; $("#qandatbl td.weight input").each(function (i, elm){ totalweight = totalweight - Number($(elm).val(), 10); }); $("#total-weight").text(totalweight); }
原文:https://stackoverflow.com/questions/14044149
满意答案
第一部分代码实际上是误导性的,并且依赖于
lena
是方形图像的事实:发生的事情等同于调用zip(range(xmax), range(ymax))
,然后将每个结果元组设置为0
。 你可以看到这里可能出现的问题:如果xmax != ymax
,那么事情将无效:>>> test = lena[:,:-3] >>> test.shape (512, 509) >>> xmax, ymax = test.shape >>> test[range(xmax), range(ymax)] = 0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: shape mismatch: objects cannot be broadcast to a single shape
定义
diag_max = min(xmax, ymax)
,然后设置lena[range(diag_max), range(diag_max)] = 0
可能会更好。第二个问题的答案更容易:
range(from, to, step)
是对range
的一般调用:>>> range(1, 10, 2) [1, 3, 5, 7, 9] >>> range(1, 10, -2) [] >>> range(10, 1, -2) [10, 8, 6, 4, 2] >>> range(10, 0, -1) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
特别是,这会反转先前的列表,因此从右到左而不是从左到右抓取对角线。
The first bit of code is actually misleading, and relies on the fact that
lena
is a square image: what happens is equivalent to callingzip(range(xmax), range(ymax))
, and then setting each of the resulting tuples to0
. You can see what could go wrong here: ifxmax != ymax
, then things won't work:>>> test = lena[:,:-3] >>> test.shape (512, 509) >>> xmax, ymax = test.shape >>> test[range(xmax), range(ymax)] = 0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: shape mismatch: objects cannot be broadcast to a single shape
It would probably be better to define
diag_max = min(xmax, ymax)
, and then setlena[range(diag_max), range(diag_max)] = 0
.The answer to your second question is easier:
range(from, to, step)
is the general call torange
:>>> range(1, 10, 2) [1, 3, 5, 7, 9] >>> range(1, 10, -2) [] >>> range(10, 1, -2) [10, 8, 6, 4, 2] >>> range(10, 0, -1) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
In particular, this reverses the previous list, and so grabs the diagonal from right to left instead of left to right.
相关问答
更多我的numpy数组总是以零结尾?(My numpy array always ends in zero?)
生成1D NumPy连续范围数组(Generate 1D NumPy array of concatenated ranges)
numpy:根据多个条件将值设置为零(numpy: Setting values to zero based on multiple conditions)
NumPy总和沿不相交索引(NumPy sum along disjoint indices)
用numpy制作一个范围列表(Make a list of ranges in numpy)
在numpy中生成缩小范围的1d数组(Generating 1d array of shrinking ranges in numpy)
使用NumPy查找条件为True的范围(Find span where condition is True using NumPy)
使用numpy将范围设置为零(Setting ranges to zero using numpy)
Numpy [...,无](Numpy […,None])
Python / Numpy:将值设置为索引范围(Python/Numpy: Setting values to index ranges)
相关文章
更多流式计算之Storm简介
[zz]流式计算之Storm简介
流式计算之Storm简介
如何构建高效的storm计算模型
Storm -- 实时计算平台
实时流式计算框架Storm 0.9.0发布通知(中文版)
【转】Spark:一个高效的分布式计算系统
storm分布式流计算引擎
【转】storm分布式流计算引擎
最新问答
更多如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
linux的常用命令干什么用的
由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
如何交换返回集中的行?(How to swap rows in a return set?)
在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
Java中的不可变类(Immutable class in Java)
WordPress发布查询(WordPress post query)
如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!