首页 \ 问答 \ 计算后总值不变(total value is not changing after calculation)

计算后总值不变(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 of 1, then total marks remaining should be 6. (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
更新时间:2024-03-26 11:03

最满意答案

第一部分代码实际上是误导性的,并且依赖于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 calling zip(range(xmax), range(ymax)), and then setting each of the resulting tuples to 0. You can see what could go wrong here: if xmax != 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 set lena[range(diag_max), range(diag_max)] = 0.

The answer to your second question is easier: range(from, to, step) is the general call to 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]

In particular, this reverses the previous list, and so grabs the diagonal from right to left instead of left to right.

相关问答

更多
  • range(0, 5)产生0, 1, 2, 3, 4 。 端点始终被省略。 你只需要range(6) 。 更好的是,使用NumPy的强大功能将该阵列制作成一行: thetamap = np.arange(6) + np.arange(6)[:,None] 这使得行矢量和列矢量,然后使用NumPy广播将它们相加在一起以形成矩阵。 range(0, 5) produces 0, 1, 2, 3, 4. The endpoint is always omitted. You want simply range( ...
  • 这是使用cumulative summation的矢量化方法 - def ranges(nv, start = 1): shifts = nv.cumsum() id_arr = np.ones(shifts[-1], dtype=int) id_arr[shifts[:-1]] = -nv[:-1]+1 id_arr[0] = start # Skip if we know the start of ranges is 1 already return id_arr ...
  • NumPy的&执行按bit-wise and 。 应用于数组时,按bit-wise and元素进行应用。 由于比较(例如r < rt )返回布尔数组,所以按bit-wise and这里的结果与logical and相同。 括号是需要的,因为NumPy's的优先级高于< 。 mask = (r < rt) & (g < gt) & (b < bt) image[mask] = 0 NumPy's & performs bit-wise and. When applied to arrays, the bit ...
  • 您可以使用np.add.reduceat作为解决此问题的一般方法。 即使范围不是全部相同的长度,这也是有效的。 要沿0轴0: [0, 25, 50]和50:75对切片进行求和,请传递索引[0, 25, 50] 50:75 [0, 25, 50] : np.add.reduceat(a, [0, 25, 50], axis=0) 此方法也可用于求和非连续范围。 例如,要对片段0:25 : 37:47 : 37:47和51:75 ,请写下: np.add.reduceat(a, [0,25, 37,47, 5 ...
  • 您可以在starts延伸到2D版本之后使用broadcasting并添加1D范围数组, x = starts[:,None] + np.arange(10) 说明 我们举一个小例子来看看这个broadcasting在这种情况下做了什么。 In [382]: starts Out[382]: array([3, 1, 3, 2]) In [383]: starts.shape Out[383]: (4,) In [384]: starts[:,None] Out[384]: array([[3], ...
  • >>> np.triu_indices(4, 1)[1] array([1, 2, 3, 2, 3, 3]) (正如@SaulloCastro指出的那样,我没有像在原始的,接受的答案中那样使用各种索引到网格网格魔法。) >>> np.triu_indices(4, 1)[1] array([1, 2, 3, 2, 3, 3]) (As pointed out by @SaulloCastro, I didn't have to use all kinds of indexing into meshgri ...
  • 怎么一个方法。 首先取你拥有的布尔数组: In [11]: a Out[11]: array([0, 0, 0, 2, 2, 0, 2, 2, 2, 0]) In [12]: a1 = a > 1 使用roll将其向左移动一个(以获得每个索引处的下一个状态): In [13]: a1_rshifted = np.roll(a1, 1) In [14]: starts = a1 & ~a1_rshifted # it's True but the previous isn't In [15]: en ...
  • 第一部分代码实际上是误导性的,并且依赖于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 ...
  • [..., None]的切片由两个“快捷方式”组成: 省略号文字组件: 点(...)表示生成完整索引元组所需的冒号。 例如,如果x是rank 5数组(即,它有5个轴),那么 x[1,2,...]相当于x[1,2,:,:,:] , x[...,3]到x[:,:,:,:,3]和 x[4,...,5,:]到x[4,:,:,5,:] 。 ( 来源 ) None组件: numpy.newaxis newaxis对象可用于所有切片操作,以创建长度为1的轴。 newaxis是'None'的别名,'None'可以代替它使用 ...
  • 以下两个都会这样做: faces[:,:,0,4] = 1 faces[:,:,0,4] = (1, 1, 1) 第一个使用NumPy将1广播到正确尺寸的所有三个值相同的事实。 第二个是更通用的,因为你可以为这三个元素分配不同的值。 Both of the following will do it: faces[:,:,0,4] = 1 faces[:,:,0,4] = (1, 1, 1) The first uses the fact that all three values are the s ...

相关文章

更多

最新问答

更多
  • Runnable上的NetworkOnMainThreadException(NetworkOnMainThreadException on Runnable)
  • C ++ 11 + SDL2 + Windows:多线程程序在任何输入事件后挂起(C++11 + SDL2 + Windows: Multithreaded program hangs after any input event)
  • AccessViolationException未处理[VB.Net] [Emgucv](AccessViolationException was unhandled [VB.Net] [Emgucv])
  • 计算时间和日期差异(Calculating Time and Date difference)
  • 以编程方式标签NSMutableAttributedString swift 4(Label NSMutableAttributedString programmatically swift 4)
  • C#对象和代码示例(C# objects and code examples)
  • 在python中是否有数学nCr函数?(Is there a math nCr function in python? [duplicate])
  • 检索R中列的最大值和第二个最大值的行名(Retrieve row names of maximum and second maximum values of a column in R)
  • 给定md5哈希时如何查找特定文件(How to find specific file when given md5 Hash)
  • Python字典因某些原因引发KeyError(Python Dictionary Throwing KeyError for Some Reason)
  • 如何让Joomla停止打开新标签中的每个链接?(How do I get Joomla to stop opening every link in a new tab?)
  • DNS服务器上的NS记录不匹配(Mismatched NS records at DNS server)
  • Python屏幕捕获错误(Python screen capture error)
  • 如何在帧集上放置div叠加?(How to put a div overlay over framesets?)
  • 页面刷新后是否可以保留表单(html)内容数据?(Is it possible to retain the form(html) content data after page refreshed?)
  • 使用iTeardownMyAppFrame和iStartMyAppInAFrame在OPA5测试中重新启动应用程序超时(Restart app within OPA5 test using iTeardownMyAppFrame and iStartMyAppInAFrame timed out)
  • 自动拆分文本内容到列(Automatically splitting text content into even columns)
  • 在r中的循环中将模型名称分配给gbm.step(assigning model names to gbm.step in loop in r)
  • 昆明哪里有电脑等级考试二级C培训?
  • C ++模板实例化,究竟是什么意思?(C++ template instantiation, what exactly does it mean?)
  • 帮助渲染来自fields_for的部分内容(Help to render a partial from fields_for)
  • 将url.action作为json对象返回mvc(return url.action as json object mvc)
  • 使用.BAT中的.application文件类型运行ac#Console App(Run a c# Console App with .application file type from a .BAT)
  • 将bindingRedirect添加到.Net标准库(Adding a bindingRedirect to a .Net Standard library)
  • Laravel版本升级会影响您的控制器吗?(Laravel version upgrade affects your controller?)
  • imaplib.error:命令SEARCH在状态AUTH中非法,只允许在SELECTED状态(imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED)
  • 如何在eclipse debug impala前端
  • 如何通过Ajax API处理多个请求?(How to handle multiple requests through an Ajax API? [closed])
  • 使用Datetime索引来分析数据框数据(Using Datetime indexing to analyse dataframe data)
  • JS 实现一个菜单效果