dictview

dict.keys()[0] on Python 3

女生的网名这么多〃 提交于 2019-11-26 20:18:58
问题 I have this sentence: 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 But when I call it this error pops: TypeError: 'dict_keys' object does not support indexing is it a version problem? I'm using Python 3.3.2 回答1: dict.keys() is a dictionary view. Just use list() directly on the dictionary instead if you need a list of keys, item 0