我坚持使用循环和字典值.
我有一个有28个键和一个列表列表的有序字典.该列表有18个元素,而每个子列表恰好有28个元素.
这些是字典键:
d.keys()
[u'ROWNO', u'crop_name', u'active', u'crop_id', u'root', u'FTR', u'FEP', u'FEI', u'FIESWP', u'FIESWI', u'PSI1', u'PSI2', u'PSI3', u'PSI4', u'BaseT', u'MiniCutT', u'C0', u'C1', u'C2', u'C3', u'BegRootD', u'MaxRootD', u'RootGC', u'NONIRR', u'IFALLOW', u'WPFSlope', u'WPFInt', u'CropPrice']
这是包含子列表的列表:
l
[[0L, u'urban', u'yes', 1L, 0.9880710363, 0.7762128576, 0.0187825216, 0.5917564943, 0.4861516696, 0.6961653309, 0.1184982939, 0.0661981243, 0.1660707814, 0.8429505723, 0.1171345066, 0.7623326175, 0.3487750229, 0.6285191844, 0.4184188771, 0.7492058959, 0.8694137433, 0.1703501437, 0.9935859838, 0.7288874143, 0.9209640764, 0.5967759748, 0.8859157749, 0.5578078404], [1L, u'field', u'no', 2L, 0.6523772182, 0.9479802477, 0.5032749979, 0.0203893553, 0.3941080805, 0.0833196002, 0.005058514, 0.3850347416, 0.6426177609, 0.4299152468, 0.7133151602, 0.2974991745, 0.436545674, 0.6156195775, 0.4132056062, 0.6906851784, 0.4214208396, 0.2687041033, 0.8325155468, 0.4442083265, 0.4998378519, 0.5186571504, 0.3647692935, 0.8424482064], [2L, u'agri', u'yes', 3L, 0.2020704208, 0.4943048221, 0.9865449923, 0.1807042379, 0.2039444118, 0.719576047, 0.8007650878, 0.8179457143, 0.387998726, 0.3547515289, 0.2082748348, 0.4387013635, 0.0721143277, 0.3136425719, 0.6693463889, 0.2584721141, 0.7619593863, 0.5423401869, 0.2358356097, 0.3030817132, 0.9207636425, 0.9602994374, 0.6839768025, 0.3863588849], [3L, u'wood', u'no', 4L, 0.3990586931, 0.1489404209, 0.3370202847, 0.7323104118, 0.2537556558, 0.2696552682, 0.1812819822, 0.7718300375, 0.1150017157, 0.4168278638, 0.4773736377, 0.1741939525, 0.2171841208, 0.3246066186, 0.7460400404, 0.7454859293, 0.9417963137, 0.4570261484, 0.8934088429, 0.7511967996, 0.4236630567, 0.6958870168, 0.1191190009, 0.0740490851], [4L, u'urban', u'yes', 5L, 0.1343765764, 0.0761525007, 0.8755807451, 0.468453757, 0.0259978706, 0.1750592026, 0.4928024118, 0.3836308129, 0.8505313466, 0.9150293856, 0.6245134636, 0.4677079234, 0.1216895832, 0.9289976482, 0.2705695813, 0.380774833, 0.3543254505, 0.9357182016, 0.0668140969, 0.6111974376, 0.0203904544, 0.0937953258, 0.7653205907, 0.9520038557], [5L, u'field', u'no', 6L, 0.5410980589, 0.2259960216, 0.7064766958, 0.9154685009, 0.1592181614, 0.7453719557, 0.9781302647, 0.8719721257, 0.7002625982, 0.6012878688, 0.1427676126, 0.3700953233, 0.2073890544, 0.273304865, 0.8485835204, 0.9888163959, 0.9482327155, 0.2982763241, 0.8209314703, 0.7865446291, 0.1269090876, 0.3201992328, 0.9890359077, 0.6757013237], [6L, u'agri', u'yes', 7L, 0.7083908082, 0.1989573038, 0.1463319648, 0.9572403287, 0.9659852618, 0.5740459575, 0.9371926109, 0.072922267, 0.2970788151, 0.9308314524, 0.0704980283, 0.0667572839, 0.0115419324, 0.3695618776, 0.4213448362, 0.4787888997, 0.3859563803, 0.7031492339, 0.62712013, 0.7668921489, 0.0638371964, 0.4407102864, 0.5829531383, 0.1342976093], [7L, u'wood', u'no', 8L, 0.0055283876, 0.3048182689, 0.4614469241, 0.0464003216, 0.0317495619, 0.8265312591, 0.6474820506, 0.992082451, 0.0494857333, 0.9159798615, 0.9043841744, 0.1447423934, 0.1647402004, 0.78561208, 0.0867245598, 0.8032354966, 0.0841638821, 0.1783657079, 0.0299151281, 0.6843691648, 0.2395078561, 0.1825108798, 0.5753446135, 0.9177717881], [8L, u'urban', u'yes', 9L, 0.9718246828, 0.7551215489, 0.216572769, 0.8816121609, 0.8941796862, 0.2406812159, 0.4491763441, 0.9773803689, 0.390031328, 0.6905167287, 0.3537031326, 0.1768295565, 0.8263032916, 0.2629455288, 0.535874563, 0.5333376324, 0.1241632847, 0.5934007231, 0.3815517791, 0.1646715028, 0.1674022009, 0.7938161173, 0.3310066026, 0.3866990861], [9L, u'field', u'no', 10L, 0.1675208267, 0.2052282211, 0.8181743179, 0.5700060991, 0.697582497, 0.1718882741, 0.756608773, 0.5778901258, 0.4517461865, 0.7875813383, 0.7578185275, 0.7281215186, 0.3673608007, 0.7612188908, 0.4462586869, 0.7133873852, 0.9872285228, 0.016436137, 0.4183363449, 0.3712834325, 0.7175842852, 0.8130433965, 0.9197248272, 0.0298202476], [10L, u'agri', u'yes', 11L, 0.9594277325, 0.9513372914, 0.7724428782, 0.8801717379, 0.1978180383, 0.4577205575, 0.2153304196, 0.7442993864, 0.1948060249, 0.8237306436, 0.9957117545, 0.5634085131, 0.3753931054, 0.6383105617, 0.9749194253, 0.5175839141, 0.5749865153, 0.4955513359, 0.1188480973, 0.7614868763, 0.4419638647, 0.6547292254, 0.9520580315, 0.8783782611], [11L, u'wood', u'no', 12L, 0.2173837037, 0.1310419382, 0.8092598945, 0.6886849501, 0.5889472859, 0.3445105369, 0.0818174037, 0.8513674382, 0.6618901114, 0.6822749798, 0.0955436809, 0.4997378506, 0.1301105707, 0.8975837422, 0.3384336666, 0.3951409762, 0.7764924613, 0.0511956604, 0.8292440753, 0.4882287942, 0.1264978535, 0.0073185936, 0.4954437898, 0.8533673952], [12L, u'urban', u'yes', 13L, 0.1859700878, 0.3730106107, 0.7632448375, 0.4986549141, 0.6685771935, 0.9393879476, 0.7763311742, 0.2643802699, 0.0672163796, 0.3476800804, 0.4229200957, 0.4999026435, 0.0022770257, 0.7704146628, 0.9350812694, 0.5622348154, 0.8106107311, 0.1275847226, 0.04681878, 0.969982317, 0.7313900809, 0.564864517, 0.1869492417, 0.5819828813], [13L, u'field', u'no', 14L, 0.6807033967, 0.675566602, 0.3012552937, 0.835614691, 0.4727382525, 0.7379879553, 0.2820282071, 0.5411274699, 0.0256456677, 0.8959374928, 0.6978217813, 0.490813799, 0.5046336369, 0.5660028895, 0.3849222322, 0.3246004835, 0.3140441144, 0.5561284195, 0.540560666, 0.143668496, 0.5226685591, 0.7229068361, 0.3684890906, 0.4579911479], [14L, u'agri', u'yes', 15L, 0.3161958808, 0.6117737556, 0.4901168528, 0.849877537, 0.6539895011, 0.1836446375, 0.9514121045, 0.398029102, 0.3159745878, 0.3169764124, 0.1558224398, 0.4183161706, 0.3339389167, 0.9637046822, 0.6701228232, 0.8212125208, 0.2478937395, 0.3155002303, 0.1098669961, 0.0692577795, 0.948612375, 0.6216475484, 0.620691183, 0.1872321395], [15L, u'wood', u'no', 16L, 0.1571277725, 0.7458446664, 0.7216782165, 0.2924881286, 0.7417769199, 0.6292481427, 0.5353465907, 0.9993602054, 0.4678665842, 0.0671332283, 0.8134182766, 0.8605326936, 0.9729331557, 0.1192704006, 0.5308676558, 0.7970071491, 0.5712603086, 0.7629612717, 0.5168838662, 0.0726957729, 0.0557336584, 0.7417301177, 0.0878397217, 0.4800303446], [16L, u'urban', u'yes', 17L, 0.7971360791, 0.6990587467, 0.4178865782, 0.9646858491, 0.4084527718, 0.9176298694, 0.0010879179, 0.6840572429, 0.0548517874, 0.4860387817, 0.2940770704, 0.1634061749, 0.2032763048, 0.6276172854, 0.9626661476, 0.1999422626, 0.9751389348, 0.2188631198, 0.8652859619, 0.2901714661, 0.1101183959, 0.3819493917, 0.7427535376, 0.6666091289], [17L, u'field', u'no', 18L, 0.2272464165, 0.7896313197, 0.8625501592, 0.2866339164, 0.1495928168, 0.1092342755, 0.6999829244, 0.4152055094, 0.1652028428, 0.9831242727, 0.5317911641, 0.9055370369, 0.7294180496, 0.3758419158, 0.0727645596, 0.6385257991, 0.2964176738, 0.5446202026, 0.286466195, 0.8451819234, 0.8354280859, 0.8926802136, 0.4593953358, 0.1168023651], [18L, u'agri', u'yes', 19L, 0.159681791, 0.7908587421, 0.3679475095, 0.6821967349, 0.0009163639, 0.0961102468, 0.3537184789, 0.6267411807, 0.2682822093, 0.2304021942, 0.0072477635, 0.9572405133, 0.6392709205, 0.1425314122, 0.4563283175, 0.5244871045, 0.2105785189, 0.2272165113, 0.3297603263, 0.0445605994, 0.8076920856, 0.5906337334, 0.1907557445, 0.9990702437]]
我想要做的是填写每个键值与子列表组合的字典值.
例如,第一个键:
d[u'ROWNO'] = [u'urban', u'field', u'agri', u'wood', u'urban', u'field', u'agri', u'wood', u'urban', u'field', u'agri', u'wood', u'urban', u'field', u'agri', u'wood', u'urban', u'field', u'agri']
我试过这个循环:
for id, k in enumerate(d.keys()):
for i in range(len(l)):
d[k] = l[i][id]
但它不起作用.
最佳答案 无需使用占位符值初始化字典,然后在之后填充值.
您可以在压缩密钥和值时从zip的返回值创建一个有序字典.
这是一个演示:
>>> from collections import OrderedDict
>>> keys = ['k1', 'k2']
>>> values = [['vA', 'vB'], ['vC', 'vD']]
>>> OrderedDict(zip(keys, values))
OrderedDict([('k1', ['vA', 'vB']), ('k2', ['vC', 'vD'])])
这种技术也适用于常规词典(当然,顺序将是任意的).
>>> dict(zip(keys,values))
{'k2': ['vC', 'vD'], 'k1': ['vA', 'vB']}
编辑响应评论:这应该做的伎俩,然后:
>>> OrderedDict(zip(keys, map(list, zip(*values))))
OrderedDict([('k1', ['vA', 'vC']), ('k2', ['vB', 'vD'])])