int
我想将'min_time'分配给min_time,'filtered_thresholds'分配给filtered_thresholds_expert,将'filtered_screens'分配给filtered_screens。
但是我得到了这个错误
min_time = j ['min_time']
TypeError:字符串索引必须是整数,而不是str
答案 0 :(得分:1)
你不需要那里的嵌套循环。您最终会迭代内部词典的键,所以当您到达错误行时,j
只是字符串'min_time'
而不是字典条目。我已经修改了你的代码,所以它不会在你的密钥中混合使用空格和下划线。看看这是否适合你。
grammar = {
'expert' : {
'min_time' : 30,
'filtered_thresholds' : {'A':10,'B':10,'C':10,'D':10,'E':20,'I':10,'T':10,'H':10,'K':0,'J':0,'L':0,'M':0,'N':0,'O':0,'P':0,'Q':0,'R':0,'S':0},
'filter_screens' : ['H','I','J','K','L','M','N','O','P','Q','R','S']
},
'expert2' : {
'min_time' : 2,
'filtered_thresholds' : {'A':10,'B':10,'C':10,'D':10,'E':20,'I':10,'T':10,'H':10,'K':0,'J':0,'L':0,'M':0,'N':0,'O':0,'P':0,'Q':0,'R':0,'S':0},
'filter_screens' : ['H','I','J','K','L','M','N','O','P','Q','R','S']
}
}
for i in grammar:
j = grammar[i]
min_time = j['min_time']
filtered_thresholds_expert = j['filtered_thresholds']
filtered_screens = j['filter_screens']
ProcessThresholds(time_per_screen_percentage)
答案 1 :(得分:1)
你不需要内循环。
for i in grammar:
j = grammar[i]
min_time = j['min_time']
filtered_thresholds_expert = j['filtered_thresholds']
filtered_screens = j['filtered_screens']
ProcessThresholds(time_per_screen_percentage)
你可以进一步完善它
for key, j in grammar.iteritems():
...
答案 2 :(得分:1)
你有几个问题:
首先,没有明显的原因grammar
是一个字典:你迭代它但从不用键索引它,所以它可能是一个列表。其次,你只需要一个循环。试试这个:
grammar = [
{
'min_time' : 30,
'filtered_thresholds' : {'A':10,'B':10,'C':10,'D':10,'E':20,'I':10,'T':10,'H':10,'K':0,'J':0,'L':0,'M':0,'N':0,'O':0,'P':0,'Q':0,'R':0,'S':0},
'filter_screens' : ['H','I','J','K','L','M','N','O','P','Q','R','S']
},
{
'min_time' : 2,
'filtered thresholds' : {'A':10,'B':10,'C':10,'D':10,'E':20,'I':10,'T':10,'H':10,'K':0,'J':0,'L':0,'M':0,'N':0,'O':0,'P':0,'Q':0,'R':0,'S':0},
'filter screens' : ['H','I','J','K','L','M','N','O','P','Q','R','S']
}
]
for i in grammar:
min_time = i['min_time']
filtered_thresholds_expert = i['filtered_thresholds']
filtered_screens = i['filter_screens']
ProcessThresholds(time_per_screen_percentage)