计算后总值不变(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.
相关问答
更多-
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 ...
-
NumPy总和沿不相交索引(NumPy sum along disjoint indices)[2022-01-11]
您可以使用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 ... -
用numpy制作一个范围列表(Make a list of ranges in numpy)[2022-04-22]
您可以在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 ...
-
Numpy [...,无](Numpy […,None])[2023-10-21]
[..., 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 ...