相关疑难解决方法(0)

Python 3上的dict.keys()[0]

我有这句话:

def Ciudad(prob):
    numero = random.random()
    ciudad = prob.keys()[0]
    for i in prob.keys():
        if(numero > prob[i]):
            if(prob[i] > prob[ciudad]):
                ciudad = i
        else:
            if(prob[i] > prob[ciudad]):
                ciudad = i

    return ciudad
Run Code Online (Sandbox Code Playgroud)

但是当我调用它时,这个错误弹出:

TypeError: 'dict_keys' object does not support indexing
Run Code Online (Sandbox Code Playgroud)

这是版本问题吗?我正在使用Python 3.3.2

python dictionary python-3.x dictview

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

标签 统计

dictionary ×1

dictview ×1

python ×1

python-3.x ×1