如何在scripting.dictionary中覆盖键的值?

时间:2018-05-29 13:08:58

标签: excel vba excel-vba

我在if循环中嵌套了以下if子句:

from typing import Tuple


def shift_distance(map_: np.ndarray,
                   *,
                   x_shift: int,
                   y_shift: int) -> Tuple[int, int]:
    """
    Converts shifts from figure coordinate system 
    to image coordinate system
    """
    x_1 = map_[0, 0, 1]
    x_2 = map_[0, -1, 1]
    y_1 = map_[0, 0, 0]
    y_2 = map_[0, -1, 0]
    d_1 = x_2 - x_1
    d_2 = y_2 - y_1
    phi = np.arctan(d_1 / d_2)
    psi = phi + np.arctan(y_shift / x_shift)
    ro = np.sqrt(x_shift ** 2 + y_shift ** 2)

    return ro * np.cos(psi), ro * np.sin(psi)

x_shift_image, y_shift_image = shift_distance(inv_query_map_1,
                                              x_shift=x_shift,
                                              y_shift=y_shift)
query_map_1[:, :, 1] -= x_shift_image.astype(int)
query_map_1[:, :, 0] -= y_shift_image.astype(int)

x_shift_image, y_shift_image = shift_distance(inv_train_map_1,
                                              x_shift=x_shift,
                                              y_shift=y_shift)
train_map_1[:, :, 1] -= x_shift_image.astype(int)
train_map_1[:, :, 0] -= y_shift_image.astype(int)

If status Like "*Active*" Then total_active = total_active + amount On Error Resume Next active_dict.Add Key:=cost_center, Item:=total_active On Error GoTo 0 end if 用于在添加On Error Resume Next字典中已存在的密钥时忽略该错误。

问题是密钥(active_dict)的值(total_active)没有得到更新。

问题是:是否可以覆盖字典键的值?

1 个答案:

答案 0 :(得分:2)

如果您使用Microsoft Scripting Dictionary,则可以使用以下内容:

If status Like "*Active*" Then
    total_active = total_active + amount
    If active_dict.Exists(cost_center) Then
        active_dict(cost_center) = total_active
    Else
        active_dict.Add Key:=cost_center, Item:=total_active
    End If
End If

甚至更好(见Kostas K评论):

If status Like "*Active*" Then
    total_active = total_active + amount
    active_dict(cost_center) = total_active
End If