我有两个列表,我试图通过Python中的列表理解来操作。我在Emacs内部的pdb调试器中执行此操作(以防万一这是问题的原因:-)。这两个列表都是从我的脚本中的数据结构派生而来的,但在我尝试使用它们时,我已将它们解析为列表。这是我在pdb中的输出,以及前面的一点测试,以显示我认为应该工作的内容(但不是吗?):
(Pdb) l1 = [100, 200, 300]
(Pdb) l2 = [1,2,3]
(Pdb) [x+y for x in l2 for y in l1]
[101, 201, 301, 102, 202, 302, 103, 203, 303]
(Pdb) data_ids = [int(i['Transaction ID']) for i in data]
(Pdb) data_ids[0]
2669299
(Pdb) len(data_ids)
707
(Pdb) type(data_ids)
<class 'list'>
(Pdb) ovr_trans_ids = [i['ovr_trans_no'] for i in overrides]
(Pdb) ovr_trans_ids[0]
2643831
(Pdb) len(ovr_trans_ids)
87
(Pdb) type(ovr_trans_ids)
<class 'list'>
(Pdb) [x+y for x in data_ids for y in ovr_trans_ids]
*** NameError: name 'ovr_trans_ids' is not defined
这两个列表推导 - 第一个使用我创建的两个简单列表l1和l2,第二个使用从我的数据派生的两个列表 - 在我看来在结构上是相同的。有人能够告诉我为什么在第二个ovr_trans_ids抛出一个NameError?您可以在之前的语句中看到它存在,是一个列表,并且有成员。还要仔细检查两个简单列表:
(Pdb) type(l1)
<class 'list'>
(Pdb) type(l2)
<class 'list'>
(Pdb)
我错过了一些非常基本的东西吗?还是怪异而深奥?
以防万一,以下是两个列表&#39;数据,只是为了证明它们并不奇怪:
(Pdb) [i for i in data_ids]
[2669299, 2669471, 2669473, 2669148, 2669154, 2669135, 2669048, 2669121, 2669146, 2669147, 2669377, 2669379, 2669581, 2669582, 2669580, 2669152, 2669443, 2669381, 2669036, 2669478, 2669481, 2669633, 2669098, 2669099, 2669158, 2669160, 2669170, 2669175, 2669180, 2669182, 2669386, 2669387, 2669274, 2669484, 2669486, 2669306, 2658733, 2658733, 2658735, 2658735, 2658739, 2658739, 2669410, 2669414, 2669199, 2669201, 2669208, 2669212, 2669211, 2669215, 2669217, 2669218, 2669283, 2669284, 2669285, 2669219, 2669220, 2669221, 2669222, 2669223, 2669225, 2669226, 2669228, 2669278, 2669279, 2669280, 2669282, 2669230, 2669234, 2669231, 2669240, 2669242, 2669249, 2669253, 2669256, 2669259, 2669260, 2669261, 2669262, 2669263, 2669266, 2669268, 2669269, 2669271, 2669272, 2669273, 2669275, 2669276, 2669277, 2669349, 2669350, 2669358, 2669359, 2669360, 2669361, 2669362, 2669366, 2669368, 2669369, 2669370, 2669372, 2669138, 2669141, 2669034, 2669035, 2669331, 2669332, 2669333, 2669334, 2669337, 2669338, 2669320, 2669325, 2669339, 2669340, 2669335, 2669207, 2669184, 2669179, 2669569, 2669232, 2669235, 2669173, 2669239, 2669290, 2669156, 2669188, 2669245, 2669191, 2669060, 2669536, 2669427, 2669102, 2669103, 2669108, 2669109, 2669110, 2669437, 2669437, 2669470, 2669431, 2669040, 2669041, 2669380, 2669596, 2669128, 2669137, 2669139, 2669140, 2669142, 2669136, 2664274, 2664274, 2664275, 2664275, 2664276, 2664276, 2668318, 2668318, 2669515, 2669517, 2669528, 2669512, 2669144, 2669143, 2669590, 2669165, 2669145, 2246915, 2246915, 2669286, 2669287, 2669289, 2669288, 2669593, 2669373, 2669382, 2669394, 2669389, 2669389, 2669398, 2669565, 2669033, 2669032, 2669475, 2669472, 2669629, 2669627, 2669088, 2669063, 2669063, 2669086, 2669122, 2669293, 2669295, 2669096, 2669167, 2669167, 2669171, 2669171, 2669168, 2669168, 2669451, 2669453, 2669456, 2669448, 2669458, 2669461, 2669462, 2669465, 2669169, 2669498, 2669500, 2669501, 2669439, 2669429, 2669134, 2669104, 2669105, 2669464, 2669509, 2669510, 2669511, 2513164, 2513164, 2669095, 2669068, 2669068, 2669095, 2669095, 2513166, 2669064, 2669064, 2513164, 2513166, 2360653, 2360653, 2360653, 2513161, 2513161, 2669085, 2669085, 2360586, 2360586, 2513162, 2513162, 2669495, 2669506, 2669476, 2669488, 2669441, 2669483, 2669497, 2669491, 2669503, 2669050, 2669296, 2669124, 2669600, 2669601, 2669395, 2669395, 2669401, 2669402, 2669294, 2669130, 2669131, 2669514, 2669066, 2669067, 2669069, 2669070, 2669071, 2669072, 2669074, 2669075, 2669076, 2669077, 2669078, 2669150, 2669123, 2669357, 2669357, 2669586, 2669589, 2669125, 2669106, 2669107, 2669236, 2669237, 2669238, 2669241, 2669246, 2669243, 2669250, 2669251, 2669255, 2669257, 2669244, 2669258, 2669037, 2669200, 2669202, 2669203, 2669204, 2669206, 2669628, 2669164, 2669210, 2669213, 2669149, 1480829, 1480829, 2668219, 2668219, 2669162, 2669205, 2669209, 2669153, 2669155, 2548599, 2548604, 2607298, 2548599, 2548604, 2607298, 2669133, 2669126, 2669489, 2669490, 2669455, 2669467, 2669436, 2669436, 2669440, 2669440, 2669479, 2669468, 2669468, 2669482, 2669383, 2669388, 2669384, 2669385, 2669151, 2444259, 2444259, 2444259, 2444259, 2662671, 2662671, 2669046, 2669045, 2669047, 2669229, 2669233, 2669267, 2669270, 2669044, 2669264, 2669038, 2669039, 2669591, 2669631, 2669630, 2669626, 2669624, 2669625, 2669610, 2669607, 2669605, 2669606, 2669603, 2669602, 2669192, 2669390, 2669391, 2669392, 2669393, 2669396, 2669397, 2669399, 2669400, 2669404, 2669405, 2669406, 2669408, 2669409, 2669411, 2669412, 2669413, 2669415, 2669416, 2669417, 2669418, 2669419, 2669420, 2669421, 2669422, 2669423, 2669424, 2669425, 2669426, 2669428, 2669430, 2669432, 2669433, 2669434, 2669438, 2669187, 2669595, 2669597, 2669598, 2669592, 2669594, 2669574, 2669575, 2669575, 2669585, 2669588, 2664826, 2664826, 2669355, 2669291, 2669292, 2669614, 2669615, 2669446, 2669079, 2669091, 2669093, 2669051, 2669058, 2669059, 2669363, 2669492, 2669493, 2669494, 2669435, 2669496, 2669281, 2669248, 2669254, 2669224, 2669364, 2669365, 2669367, 2669371, 2669341, 2669342, 2669344, 2669347, 2669348, 2669351, 2669353, 2669053, 2669054, 2669055, 2669057, 2473297, 2473297, 2473297, 2473297, 2669336, 2669056, 2669062, 2669062, 2669065, 2669065, 2669065, 2669513, 2669527, 2669531, 2669531, 2669552, 2669553, 2669214, 2669161, 2669119, 2669127, 2669469, 2669314, 2669321, 2669326, 2669117, 2669118, 2669120, 2669599, 2669445, 2669297, 2669298, 2669300, 2669302, 2669305, 2669307, 2669459, 2669463, 2669195, 2669197, 2669193, 2669194, 2669176, 2669185, 2669172, 2669252, 2669522, 2669544, 2669546, 2669549, 2669578, 2669579, 2669587, 2669576, 2669584, 2669583, 2669374, 2669375, 2669634, 2669577, 2669129, 2669635, 2669356, 2669356, 2669555, 2669556, 2669558, 2669061, 2669301, 2669112, 2669113, 2669504, 2669505, 2669507, 2669508, 2669442, 2669159, 2669447, 2669449, 2669450, 2669452, 2669454, 2669457, 2669460, 2669444, 2668514, 2668514, 2668533, 2668533, 2668528, 2668528, 2668539, 2668539, 2668541, 2668541, 2668543, 2668543, 2668551, 2668551, 2668557, 2668557, 2668560, 2668560, 2668562, 2668562, 2668571, 2668571, 2669216, 2669378, 2669477, 2669480, 2669485, 2669354, 2499080, 2499080, 2669043, 2669042, 2669376, 2669466, 2669407, 2669474, 2669311, 2669311, 2669343, 2669318, 2669318, 2669345, 2669346, 2669352, 2669265, 2669265, 2669196, 2669247, 2669190, 2669177, 2669609, 2669621, 2669620, 2669618, 2669616, 2669617, 2669608, 2669613, 2669611, 2669612, 2669623, 2669622, 2669521, 2669534, 2669541, 2669557, 2669559, 2669502, 2669550, 2669561, 2669563, 2669563, 2669551, 2669554, 2669516, 2669518, 2669519, 2669520, 2669523, 2669524, 2669525, 2669526, 2669529, 2669530, 2669532, 2669533, 2669535, 2669537, 2669538, 2669539, 2669540, 2669542, 2669543, 2669545, 2669547, 2669548, 2669604, 2669309, 2669310, 2669312, 2669313, 2669315, 2669316, 2669317, 2669319, 2669322, 2669323, 2669324, 2669327, 2669328, 2669329, 2669330, 2669049, 2669308, 2669087, 2669089, 2669090, 2669084, 2669092, 2669082, 2669094, 2669101, 2669097, 2669100, 2669487, 2669304, 2669081, 2669080, 2669083, 2669163, 2669166, 2669111, 2669114, 2669560, 2669562, 2669115, 2669116, 2669632, 2669572, 2669499, 2669619, 2669403, 2669566, 2669567, 2669564, 2669073, 2669052, 2669303, 2669568, 2669570, 2669571, 2669573, 2669174, 2669174, 2669181, 2669178, 2669183, 2669198, 2669157, 2669132, 2669227, 2669186, 2669189]
(Pdb) [i for i in ovr_trans_ids]
[2643831, 2643829, 2643833, 2643834, 2643827, 2643379, 2643011, 2643014, 2643016, 2643004, 2643019, 2643020, 2643023, 2643022, 2643026, 2643038, 2643043, 2643032, 2643036, 2642102, 2640722, 2642066, 2641792, 2641761, 2641925, 2641762, 2641783, 1625044, 1642214, 1690503, 1744960, 1758411, 1762104, 1818109, 1876448, 1927152, 1978012, 1992422, 1992863, 2036783, 2090400, 2134492, 2176780, 2187001, 2193071, 2229660, 2285643, 2329190, 2368459, 2381546, 2383473, 2423428, 2479972, 2522333, 2564597, 2577734, 2578759, 2614252, 2580012, 2114716, 2125297, 2644874, 2644883, 2644884, 2644887, 2644888, 2644889, 2644618, 2647702, 2652217, 2652221, 2652999, 2669309, 2669310, 2669312, 2669313, 2669315, 2669316, 2669317, 2669319, 2669322, 2669323, 2669324, 2669327, 2669328, 2669329, 2669330]