相关疑难解决方法(0)

TypeError: 'dict_keys' object does not support indexing

def shuffle(self, x, random=None, int=int):
    """x, random=random.random -> shuffle list x in place; return None.

    Optional arg random is a 0-argument function returning a random
    float in [0.0, 1.0); by default, the standard random.random.
    """

    randbelow = self._randbelow
    for i in reversed(range(1, len(x))):
        # pick an element in x[:i+1] with which to exchange x[i]
        j = randbelow(i+1) if random is None else int(random() * (i+1))
        x[i], x[j] = x[j], x[i]
Run Code Online (Sandbox Code Playgroud)

When I run the shuffle function it raises the following …

python dictionary python-3.x

135
推荐指数
3
解决办法
11万
查看次数

如何索引到字典?

我在下面有一个词典:

colors = {
    "blue" : "5",
    "red" : "6",
    "yellow" : "8",
}
Run Code Online (Sandbox Code Playgroud)

如何索引字典中的第一个条目?

colors[0]将返回一个KeyError明显的原因.

python indexing dictionary

72
推荐指数
2
解决办法
28万
查看次数

Django Aggreagtion:仅返回值?

我有一个已支付的价值清单,并希望显示已支付的总金额.我使用Aggregation和Sum来一起计算值.问题是,我只想要打印出总值,但是聚合打印出:( {'amount__sum': 480.0}480.0是总增加值.

在我看来,我有:

    from django.db.models import Sum

    total_paid = Payment.objects.all.aggregate(Sum('amount'))
Run Code Online (Sandbox Code Playgroud)

为了显示页面上的值,我有一个mako模板,其中包含以下内容:

    <p><strong>Total Paid:</strong> ${total_paid}</p>
Run Code Online (Sandbox Code Playgroud)

如何让它显示480.0而不是{'amount__sum': 480.0}

python django sum aggregation

24
推荐指数
3
解决办法
2万
查看次数

类型错误:“dict_values”对象不可下标

best_params_train = dict(optimizer=optimizers[0], learning_rate=learning_rates[0],
                         cnn_train_type=cnn_train_types[0], 
                         cnn_arch=cnns_arch.values()[0],
                         dropout=dropouts[0])
Run Code Online (Sandbox Code Playgroud)

它在 cnn_arch=cnns_arch.values()[0] 处给出错误 TypeError: 'dict_values' object is not subscriptable。我尝试转换为列表但没有成功。如何将上面的 dict(....) 转换为列表

exp_params_train = dict(optimizer=optimizers[1:], learning_rate=learning_rates[1:],
                        cnn_train_type=cnn_train_types[1:], dropout=dropouts[1:],
                        cnn_arch=cnns_arch.values())
Run Code Online (Sandbox Code Playgroud)

python dictionary python-3.x

10
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×4

dictionary ×3

python-3.x ×2

aggregation ×1

django ×1

indexing ×1

sum ×1