计算后总值不变(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.

相关问答

更多

我的numpy数组总是以零结尾?(My numpy array always ends in zero?)

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 r...

生成1D NumPy连续范围数组(Generate 1D NumPy array of concatenated ranges)

这是使用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 i...

numpy:根据多个条件将值设置为零(numpy: Setting values to zero based on multiple conditions)

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, th...

NumPy总和沿不相交索引(NumPy sum along disjoint indices)

您可以使用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,...

用numpy制作一个范围列表(Make a list of ranges in numpy)

您可以在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([[...

在numpy中生成缩小范围的1d数组(Generating 1d array of shrinking ranges in numpy)

>>> 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 me...

使用NumPy查找条件为True的范围(Find span where condition is True using NumPy)

怎么一个方法。 首先取你拥有的布尔数组: 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...

使用numpy将范围设置为零(Setting ranges to zero using numpy)

第一部分代码实际上是误导性的,并且依赖于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 Trace...

Numpy [...,无](Numpy […,None])

[..., 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'可以...

Python / Numpy:将值设置为索引范围(Python/Numpy: Setting values to index ranges)

以下两个都会这样做: 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 ...

相关文章

更多

流式计算之Storm简介

http://blog.sina.com.cn/s/blog_406d9bb00100ui5p.htm ...

[zz]流式计算之Storm简介

转载自:http://blog.sina.com.cn/s/blog_406d9bb00100ui5p ...

流式计算之Storm简介

Storm是一个分布式的、容错的实时计算系统,遵循Eclipse Public License 1.0 ...

如何构建高效的storm计算模型

计算机制简介 Storm采用流式计算的模型,和shell类似让数据在一个个“管道”中进行处理。 S ...

Storm -- 实时计算平台

1.1 实时流计算 互联网从诞生的第一时间起,对世界的最大的改变就是让信息能够实时交互,从而大大加速了 ...

实时流式计算框架Storm 0.9.0发布通知(中文版)

Storm0.9.0发布通知中文翻译版(2013/12/10 by 富士通邵贤军 有错误一定告诉我 s ...

【转】Spark:一个高效的分布式计算系统

原文地址:http://tech.uc.cn/?p=2116 概述 什么是Spark Spark是 ...

storm分布式流计算引擎

场景 伴随着信息科技日新月异的发展,信息呈现出爆发式的膨胀,人们获取信息的途径也更加多样、更加便捷, ...

【转】storm分布式流计算引擎

场景 伴随着信息科技日新月异的发展,信息呈现出爆发式的膨胀,人们获取信息的途径也更加多样、更加便捷,同 ...

最新问答

更多

如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)

请尝试以下方法: $messages = $user->contact_messages()->paginate(10); Try the following: $messages = $user->contact_messages()->paginate(10);

linux的常用命令干什么用的

linux和win7不一样,win7都图形界面,都是用鼠标来操作打开,解压,或者关闭。而linux是没有图形界面的,就和命令提示符一样的一个文本框,操作linux里的文件可没有鼠标给你用,打开文件夹或者解压文件之类的操作都要通过linux常用命令。

由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)

它创建了这些视图: 'auth/login.blade.php', 'auth/register.blade.php', 'auth/passwords/email.blade.php', 'auth/passwords/reset.blade.php', 'layouts/app.blade.php', 'home.blade.php' 并修改这些文件: 'Http/Controllers/HomeController.php', 'routes/web.php' 如果使用--views

如何交换返回集中的行?(How to swap rows in a return set?)

您可以使用特殊的CASE表达式进行ORDER BY如下所示: SELECT * FROM table ORDER BY CASE id WHEN 3 THEN 8 WHEN 8 THEN 3 ELSE id END You can ORDER BY using a special CASE expression like this: SELECT * FROM table ORDER BY CASE id WHEN 3 THEN 8 WHEN 8 THEN 3 ELSE id END

在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)

我建议你制作一个新的TableView Cell,在这个newTableViewCell中,加载第2节中的所有单元格,然后为newTableViewCell提供边框。 但如果您不想这样做,那么您可以使用以下代码: @property(strong,nonatomic) CAShapeLayer* borderPath; -(void)viewDidLayoutSubviews{ [_borderPath removeFromSuperlayer]; UIView *viewToGiveBord

使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)

出于一些奇怪的原因,我现在发现了一个不同的问题, Boost.Spirit SQL语法/词法分析失败 ,其中提供了一些其他解决方案来进行空格跳过。 一个更好的! 以下是根据建议重新编写的示例代码: #include #include #include #include #include #incl

Java中的不可变类(Immutable class in Java)

1.如果我只有final变量的课程? 这会让你远离但不是全部。 这些变量的类型也需要是不可变的。 考虑一下 class MyImmutableClass { // final variable, referring to a mutable type final String[] arr = { "hello" }; // ... } 这允许有人做 myImmutableObject.arr[0] = "world"; 并有效地改变你的不可变类的对象。 此外,建议禁

WordPress发布查询(WordPress post query)

如果你想要在div中包含所有帖子: post_parent) { $myposts = get_posts('numberposts=10&tag=medical&order=DESC'); echo ' '; foreach ($myposts as $post): ?>

如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)

我不确定哪个是MySQL的正确答案,因为它本身还不支持IPv6地址格式(尽管“ WL#798:MySQL IPv6支持 ”表明它将在MySQL v6.0中,当前文档不支持)。 不过,你建议的人建议去2 * BIGINT,但要确保他们是UNSIGNED。 在IPv6的/ 64地址边界处有一种自然分割(因为/ 64是最小的网格块大小),这将与其很好地对齐。 I'm not sure which is the right answer for MySQL given that it doesn't y

是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)

您可以使用Object.keys和Array#find来获取匹配value的key 。 const letters = {a:'26',b:'25',c:'24',d:'23',e:'22',f:'21',g:'20',h:'19',i:'18',j:'17',k:'16',l:'15',m:'14',n:'13',o:'12',p:'11',q:'10',r:'9',s:'8',t:'7',u:'6',v:'5',w:'4',x:'3',y:'2',z:'1'}; function sw