如果我不知道“albert”是驴还是斑马,我该如何改变他的“空间”?
self.object_attr = {'donkey': {
'name': 'roger', 'zone': 'forrest', 'space': [0, 0]}{
'name': 'albert', 'zone': 'forrest', 'space': [1, 1]}
'zebra': {
'name': 'pain', 'zone': 'forrest', 'space': [0, 0]}{
'name': 'alesha', 'zone': 'forrest', 'space': [1, 1]}}
print self.object_attr
for species in data.values():
for animal in species:
if animal['name'] == 'albert':
animal['space'] = [50, 50]
#output this is what object_attr is
{'donkey': {'roger': {'zone': 'forrest', 'space': [0, 0]}, 'albert': {'zone': 'forrest', 'space': [0, 0]}}}
#the error occurs on the "if animal['name'] == 'albert':" line
TypeError: string indices must be integers, not str