Python数组问题 - 字符串索引必须是整数而不是元组

时间:2015-12-19 15:08:19

标签: python arrays numpy

请考虑以下代码:

handInformation = [
"Thumb"[
"MetaCarpal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]], 
"Proximal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]],
"Intermediate"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]],
"Distal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]]], 

"Index"[
"MetaCarpal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]], 
"Proximal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]],
"Intermediate"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]],
"Distal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]]], 

"Middle"[
"MetaCarpal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]], 
"Proximal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]],
"Intermediate"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]],
"Distal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]]], 

"Ring"[
"MetaCarpal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]], 
"Proximal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]],
"Intermediate"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]],
"Distal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]]], 

"Pinky"[
"MetaCarpal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]], 
"Proximal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]],
"Intermediate"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]],
"Distal"["start"[0,0,0], "end"[0,0,0], "direction"[0,0,0]]]]

我遇到类型错误string indices must be integers, not tuple

自从我上次使用Python以来,已经有一段时间了以前总是和NumPy一起工作但是我不是。

基本上我试图捕获的数据是每只手上每个骨骼的三组三个值。

如果有人能够解释错误发生的原因,以及任何解决方法(最好不使用NumPy,但我愿意接受建议),我们将不胜感激。

修改

我希望用以下类似的方式访问数据:

        for finger in hand.fingers:
            for x in range(0,4):
                bone = finger.bone(x)
                value1 = handInformation[finger][bone][0]
                value2 = handInformation[finger][bone][1]
                value3 = handInformation[finger][bone][2]

1 个答案:

答案 0 :(得分:1)

除了字典格式的数据的另一个答案,你可以像这样访问数据

for finger, fingerInfo in handInformation.items():
    for bone, boneInfo in fingerInfo.items():
        start = boneInfo['start']
        end = boneInfo['end']
        direction = boneInfo['direction']