我有一个字典文件如下:
dict4={4: 1.2000000000000455, 8: 18.028571428571468, 15: 10.542857142857088, 18: -10.628571428571263, 24: -151.80000000000007, 28: -0.17142857142857792, 35: -0.599999999999909, 41: -0.8571428571428896, 48: -1.2857142857143344, 55: -1.8857142857141298, 124: 1.3714285714285097, 131: 0.5142857142857338, 138: 0.7714285714286007, 144: 1.7142857142856656, 151: 1.5428571428572013, 158: 0.5142857142857338, 161: 151.80000000000007, 171: -167.62857142857138, 181: -0.17142857142857792, 201: -0.34285714285715585, 208: -1.7999999999999545, 215: -8.657142857142844, 225: 3.342857142857156, 231: 1.9714285714285325, 238: 1.2000000000000455, 245: 1.5142857142857338, 248: 0.08571428571428896, 253: 0.08571428571428896, 258: 0.2571428571429806, 268: 167.62857142857138, 275: 8.914285714285711, 325: -8.314285714285688, 331: -4.2000000000000455, 338: -0.6000000000000227, 342: -203.02857142857135, 348: -6.428571428571445, 411: 1.7142857142856656, 418: 1.457142857143026, 425: 1.7999999999999545, 431: 0.7714285714286007, 435: 168.28571428571433, 445: -168.28571428571433, 451: -0.34285714285715585, 455: -5.628571428571377, 462: -4.028571428571581, 468: -5.142857142857224, 498: 1.7999999999999545, 505: 1.5428571428572013, 511: 1.4571428571427987, 518: 1.6285714285714903, 525: 0.9428571428571786, 531: 0.17142857142857792, 535: 4.685714285714312, 542: 175.88571428571436, 551: -6.942857142857179, 558: -7.457142857142799, 562: -0.5142857142857338, 567: -0.4285714285714448, 572: -1.1999999999999318, 578: -151.60000000000025, 582: -0.5142857142856201, 589: -7.1142857142857565, 605: 1.5428571428572013, 612: 0.34285714285715585, 618: 0.771428571428487, 625: 0.599999999999909, 629: 151.60000000000025, 638: -10.199999999999818, 645: -175.88571428571436, 649: -0.771428571428487, 655: -1.0285714285714675, 662: -1.8000000000000682, 669: -4.1142857142857565, 705: -0.5142857142857338, 709: -23.91428571428571, 718: 2.571428571428555, 725: 0.2571428571428669, 732: 0.34285714285715585, 738: 0.2571428571428669, 742: 117.25714285714275, 752: -117.25714285714275, 755: -1.1142857142856428, 762: -2.1428571428571104, 797: -2.828571428571422, 801: -38.82857142857142, 857: 4.714285714285552, 864: 1.9714285714286461, 871: 1.771428571428487, 874: 0.08571428571428896, 881: 181.7714285714285, 888: 8.914285714285711, 897: -171.19999999999982, 901: -0.5142857142857338, 908: -3.085714285714289, 914: -8.400000000000205, 924: 3.6000000000001364, 931: 1.9714285714284188, 937: 0.9428571428571786, 944: 0.17142857142857792, 954: 157.42857142857144, 964: -157.42857142857144, 971: -3.5142857142857338, 974: -0.08571428571428896, 981: -0.2571428571427532, 988: -0.34285714285704216, 994: -4.2000000000000455, 1018: 3.257142857142867, 1024: 2.142857142856883, 1031: 1.6285714285713766, 1038: 0.8571428571428896, 1048: 171.19999999999982, 1055: 7.457142857142912, 1064: -7.0285714285714675, 1071: -5.62857142857149, 1075: -0.34285714285704216, 1079: -0.08571428571417528, 1084: -0.685714285714198, 1091: -0.7714285714286007, 1098: -0.17142857142857792, 1101: -174.62857142857126, 1108: -1.8857142857142435, 1115: -4.457142857142799, 1164: 0.8571428571428896, 1168: 136.88571428571424, 1184: -5.228571428571286, 1191: -0.34285714285715585, 1195: -136.88571428571424, 1201: -7.7999999999999545, 1218: 1.5428571428572013, 1224: 0.685714285714198, 1231: 0.2571428571428669, 1235: 140.05714285714282, 1241: 140.05714285714282, 1251: -140.05714285714282, 1255: -0.7714285714286007, 1261: -0.9428571428570649, 1268: -1.1142857142857565, 1275: -6.51428571428562, 1291: 1.1142857142856428, 1298: 0.6000000000000227, 1304: 0.2571428571428669, 1308: 5.800000000000068, 1313: 0.34285714285715585, 1318: 2.8285714285713084, 1324: 117.54285714285697, 1335: 0.5142857142857338, 1342: 3.257142857142867, 1348: 8.914285714285597}
在这里,正浮点值序列和负浮点值序列彼此交替。
我想创建一个嵌套列表,它捆绑了交替的正浮点数和负浮点数。下面是我为此编写的python代码。
count=0
list1=[]
list2=[]
list3=[]
for i, j in dict4.items():
print(j)
if j>0:
if len(list3)==count:
list1.append((i, j))
else:
list1.append((i, j))
list3.append(list2)
list2.clear()
count+=2
elif j<0:
if len(list3)==count:
list3.append(list1)
list2.append((i, j))
list1.clear()
else:
list2.append((i, j))
结果如下:
[[(4, 1.2000000000000455), (8, 18.028571428571468), (15, 10.542857142857088)], [(18, -10.628571428571263), (24, -151.80000000000007), (28, -0.17142857142857792), (35, -0.599999999999909), (41, -0.8571428571428896), (48, -1.2857142857143344), (55, -1.8857142857141298)], [(124, 1.3714285714285097), (131, 0.5142857142857338), (138, 0.7714285714286007), (144, 1.7142857142856656), (151, 1.5428571428572013), (158, 0.5142857142857338), (161, 151.80000000000007)], [(171, -167.62857142857138), (181, -0.17142857142857792), (201, -0.34285714285715585), (208, -1.7999999999999545), (215, -8.657142857142844)], [(225, 3.342857142857156), (231, 1.9714285714285325), (238, 1.2000000000000455), (245, 1.5142857142857338), (248, 0.08571428571428896), (253, 0.08571428571428896), (258, 0.2571428571429806), (268, 167.62857142857138), (275, 8.914285714285711)], [(325, -8.314285714285688), (331, -4.2000000000000455), (338, -0.6000000000000227), (342, -203.02857142857135), (348, -6.428571428571445)], [(411, 1.7142857142856656), (418, 1.457142857143026), (425, 1.7999999999999545), (431, 0.7714285714286007), (435, 168.28571428571433)], [(445, -168.28571428571433), (451, -0.34285714285715585), (455, -5.628571428571377), (462, -4.028571428571581), (468, -5.142857142857224)], [(498, 1.7999999999999545), (505, 1.5428571428572013), (511, 1.4571428571427987), (518, 1.6285714285714903), (525, 0.9428571428571786), (531, 0.17142857142857792), (535, 4.685714285714312), (542, 175.88571428571436)], [(551, -6.942857142857179), (558, -7.457142857142799), (562, -0.5142857142857338), (567, -0.4285714285714448), (572, -1.1999999999999318), (578, -151.60000000000025), (582, -0.5142857142856201), (589, -7.1142857142857565)], [(605, 1.5428571428572013), (612, 0.34285714285715585), (618, 0.771428571428487), (625, 0.599999999999909), (629, 151.60000000000025)], [(638, -10.199999999999818), (645, -175.88571428571436), (649, -0.771428571428487), (655, -1.0285714285714675), (662, -1.8000000000000682), (669, -4.1142857142857565), (705, -0.5142857142857338), (709, -23.91428571428571)], [(718, 2.571428571428555), (725, 0.2571428571428669), (732, 0.34285714285715585), (738, 0.2571428571428669), (742, 117.25714285714275)], [(752, -117.25714285714275), (755, -1.1142857142856428), (762, -2.1428571428571104), (797, -2.828571428571422), (801, -38.82857142857142)], [(857, 4.714285714285552), (864, 1.9714285714286461), (871, 1.771428571428487), (874, 0.08571428571428896), (881, 181.7714285714285), (888, 8.914285714285711)], [(897, -171.19999999999982), (901, -0.5142857142857338), (908, -3.085714285714289), (914, -8.400000000000205)], [(924, 3.6000000000001364), (931, 1.9714285714284188), (937, 0.9428571428571786), (944, 0.17142857142857792), (954, 157.42857142857144)], [(964, -157.42857142857144), (971, -3.5142857142857338), (974, -0.08571428571428896), (981, -0.2571428571427532), (988, -0.34285714285704216), (994, -4.2000000000000455)], [(1018, 3.257142857142867), (1024, 2.142857142856883), (1031, 1.6285714285713766), (1038, 0.8571428571428896), (1048, 171.19999999999982), (1055, 7.457142857142912)], [(1064, -7.0285714285714675), (1071, -5.62857142857149), (1075, -0.34285714285704216), (1079, -0.08571428571417528), (1084, -0.685714285714198), (1091, -0.7714285714286007), (1098, -0.17142857142857792), (1101, -174.62857142857126), (1108, -1.8857142857142435), (1115, -4.457142857142799)], [(1164, 0.8571428571428896), (1168, 136.88571428571424)], [(1184, -5.228571428571286), (1191, -0.34285714285715585), (1195, -136.88571428571424), (1201, -7.7999999999999545)], [(1218, 1.5428571428572013), (1224, 0.685714285714198), (1231, 0.2571428571428669), (1235, 140.05714285714282), (1241, 140.05714285714282)], [(1251, -140.05714285714282), (1255, -0.7714285714286007), (1261, -0.9428571428570649), (1268, -1.1142857142857565), (1275, -6.51428571428562)]]
在输出中,它没有考虑具有1291、
{1291: 1.1142857142856428, 1298: 0.6000000000000227, 1304: 0.2571428571428669, 1308: 5.800000000000068, 1313: 0.34285714285715585, 1318: 2.8285714285713084, 1324: 117.54285714285697, 1335: 0.5142857142857338, 1342: 3.257142857142867, 1348: 8.914285714285597}
有人能帮我找出代码中的错误吗?
2条答案
按热度按时间vi4fp9gy1#
因此,您的代码中存在的问题是,您正在向list1添加最后一个元素,但没有将这些元素最终追加到list3。您可以添加一个检查,以查看list1或list2中是否有任何元素,并将其作为最终追加添加到list3。
然而,整理一下你的逻辑,你可以这样做:
u5i3ibmn2#
谢谢你@Andrew Ray,帮我找到了代码中的问题。也谢谢你的代码,非常简洁,更像Python。尽管如此,我还是对自己的代码做了一些小小的修改,现在它确实可以工作了。