l would like to get access to a specific values of my dictionary given their indices. When l given only one index or a continuing list of indices [0:30] it works as follow:
print(labels.keys()[0])
'IceDancing'
print(labels.keys()[0:3])
['IceDancing', 'IceDancing', 'HighJump']
However when l try to access specific indices as follow :
indices=[3,4,45,8,56]
labels.keys()[indices]
l get the following error
*** TypeError: list indices must be integers, not list
Thank you for your help
As mentioned, dictionaries in Python 2.7 are not ordered. You should not assume any particular or consistent order when you use dict.keys
.
Nonetheless, what you are requesting is possible with operator.itemgetter
. Here's a trivial example:
from operator import itemgetter
d = {i+10: i for i in range(100)}
idx = [3,4,45,8,56]
print itemgetter(*idx)(d.keys())
# (13, 14, 55, 18, 66)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments