Python中OrderedDict字符串元素索引结果及指定代码中max_wait_time的取值与运行可行性问询
Hey there! Let's break down your two Python questions one by one:
First off, remember that OrderedDict (from the collections module) is just a dict that preserves insertion order—it behaves almost exactly like a regular Python dictionary in most ways. When you use a string as an index (aka a key) to access an OrderedDict, you'll get the corresponding value tied to that key, just like with a standard dict.
For example:
from collections import OrderedDict my_od = OrderedDict([('EAF', [1]), ('AOD', [2]), ('LF', [3])]) print(my_od['EAF']) # Output: [1] print(my_od['LF']) # Output: [3]
If you try to index with a string that's not a key in the OrderedDict, you'll get a KeyError—same as a normal dict.
Let's start with the hard truth: this code won't run as-is because of a couple of critical errors. Let's walk through them, then fix the code to explain the logic you're curious about.
First error: Wrong type for rescat2idx
You defined rescat2idx as a list (wrapped in square brackets, with an extra 174 at the end), but lists don't have an items() method. When you try to loop with for res_cat, resources in rescat2idx.items():, Python will throw an AttributeError: 'list' object has no attribute 'items'.
I suspect you meant to assign rescat2idx directly to the OrderedDict (drop the outer square brackets and the trailing 174).
Second error: Typo in variable name
You created a dict called trans_time_max, but in the loop you wrote time_trans_max['TR_S%d' % ...]—missing an s in the variable name. This will cause a NameError: name 'time_trans_max' is not defined.
Fixed code and max_wait_time logic
If we fix those two issues, here's the working code:
from collections import OrderedDict rescat2idx = OrderedDict([ ('EAF', [1]), ('AOD', [2]), ('LF', [3]), ('CC1', [4]), ('CC2', [5]), ('H_A_S1', range(6, 30)), ('H_A_S2', range(30, 54)), ('H_B_S2', range(54, 78)), ('H_A_S3', range(78, 102)), ('H_B_S3', range(102, 126)), ('H_A_S4', range(126, 150)), ('H_B_S4', range(150, 174)), ('EN', [174]) ]) trans_time = {'TR_S1': 10, 'TR_S2': 4, 'TR_S3': 10} trans_time_max = {'TR_S1': 240, 'TR_S2': 240, 'TR_S3': 120} for res_cat, resources in rescat2idx.items(): if 'H_B_' not in res_cat: continue max_wait_time = trans_time_max['TR_S%d' % (int(res_cat[-1]) - 1)] print(f"res_cat: {res_cat}, max_wait_time: {max_wait_time}")
Now let's clarify the max_wait_time logic you're confused about:
res_catis a string key from theOrderedDict—the ones that pass the'H_B_'check are'H_B_S2','H_B_S3','H_B_S4'res_cat[-1]grabs the last character of the string: for'H_B_S2', that's'2'; for'H_B_S3', it's'3'; for'H_B_S4', it's'4'- We convert that character to an integer, subtract 1:
int('2') -1 =1,int('3')-1=2,int('4')-1=3 - That number gets plugged into
'TR_S%d'to make keys like'TR_S1','TR_S2','TR_S3' - Finally, we look up those keys in
trans_time_maxto get the max wait time
When you run the fixed code, the output will be:
res_cat: H_B_S2, max_wait_time: 240 res_cat: H_B_S3, max_wait_time: 240 res_cat: H_B_S4, max_wait_time: 120
内容的提问来源于stack exchange,提问作者Margi Shah

