我试图创建一个q表作为一个字典,在grasshopper(使用IronPython作为解释器的参数化设计工具)中填充随机值。当我输入如图1所示的代码时,我收到如图2所示的字典。键显然是不正确的,例如,第二个键包含一个7作为元组中的第一个元素。该范围中的许多值也丢失了。我在蚱蜢外面尝试了相同的代码,我收到了预期的字典(图3)。有人知道什么可能是错的吗?也许要和泡菜做点什么?由于
code
if not os.path.exists(r'C:mygameqtable.pickle'):
q_table = {}
for i in range(-4, 5):
for ii in range(-4, 5):
q_table[(i, ii)] = [random.uniform(-5, 0) for i in range(8)]
else:
with open(r'C:mygameqtable.pickle', 'rb') as g:
q_table = pickle.load(g)
print q_table
Output
{(4, -4): [-3.520083815553825, -2.3704751883184945, -4.0685524123832355, -3.4927001439899468, -4.3923893144207007, -0.019833855474534978, -3.5860222182492802, -2.2406018480736938], (7, 0): [-4.7920929574335487, -3.0601559625792958, -1.4715996072020343, -1.0487425223064415, -4.4187855611197886, -1.1312416456611891, -0.67451461958274272, -3.164000700856664], (7, -3): [-3.8802937111278273, -0.69118188223808019, -1.891694841453587, -3.6256369185311605, -3.706751355172293, -1.1751120184789228, -2.068736659932688, -1.3092753002299244], (7, 3): [-3.7771045458768766, -0.66948593131998457, -0.15252211747111932, -3.9578565980116154, -0.84661690802984957, -0.47886339305807635, -2.2465660346974916, -0.97015198441635597], (7, 1): [-3.295437585979935, -2.5247683251734299, -1.5386199607196649, -2.3074561626323442, -3.2523977539026001, -2.2941147083618847, -4.8581184994141138, -4.3086735321065888], (-4, -4): [-2.9006128994707185, -1.0355895831133388, -4.2314504474149652, -1.5426359093130664, -1.9119367635444124, -0.98450396407743135, -1.1493616076809472, -2.6627710034649814], (-3, -4): [-2.6437773379373564, -3.1273152830999935, -0.2345719981788843, -1.6344121351211682, -1.7156187275121515, -2.5124012249125096, -3.8482003400963158, -1.8655572269549525], (-2, -4): [-4.6778971552658222, -1.2055147713233798, -2.1132753199517031, -3.7856931941520373, -0.28909698800912409, -4.1796566689273424, -2.1405309748662122, -0.71350798505461199], (-1, -4): [-1.1196196746564464, -3.9428871642243934, -0.30852331221399343, -3.2726712904725797, -0.56419014343011842, -3.0034710954865966, -4.0346254931572503, -0.0093634288304667024], (7, -1): [-1.2311124775680007, -4.0508269909267547, -4.2408440151986255, -3.5767946197712264, -3.9417929596666141, -4.7788993113215428, -2.5827432701636868, -2.4437964249378004], (7, -2): [-1.0058926909459522, -4.7213608287072759, -1.5505657597277631, -0.035928647971857686, -1.1991837393210343, -3.991745086339376, -3.0453744022544282, -2.4332291077214623], (1, -4): [-3.1230218477123191, -2.8783078411392133, -1.7657085813198794, -4.4968865044068078, -1.8197692149843854, -1.5213667810937244, -4.1551081218366974, -0.74959410913214342], (7, 4): [-2.3283546947550766, -2.3264493155802675, -2.8339629062558624, -1.059230891067596, -1.2725434310621226, -0.62252825886972918, -2.903700479959515, -0.65274906172347258], (2, -4): [-1.7295329140896731, -2.882707555477277, -3.9116014289066166, -3.536228175873497, -3.7453043250483842, -4.0655001804922701, -3.4949360265896594, -3.8391288817687625], (3, -4): [-2.8190938037631783, -1.1150629961702596, -2.0152863023603684, -1.9583558978406748, -4.3518540160757313, -1.4772091206644209, -0.61540843798075784, -4.2891375097383611], (7, 2): [-0.10967639712811206, -2.7495850514288622, -2.594249907007093, -4.6154060033647371, -4.9716965010668908, -0.83056394661160571, -2.5912392190274107, -1.9203136396312011], (0, -4): [-1.5790973392637859, -4.6842949852541587, -3.3882469495041887, -4.6853648706263531, -1.2170246638493203, -0.20947243651549119, -1.8708824441525125, -3.314007325820401]}
我建议您尝试在列表推导中使用不同的变量名称,如"n"而不是"i"一次。
q_table[(i, ii)] = [random.uniform(-5, 0) for n in range(8)]
字典也是不排序的。根据不同的环境,您的"打印输出";可以按"希望"的任意顺序列出键。