I have a dictionary file as given below.
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}
Here, sequences of positive and negative floating-point values alternate with each other. I want to create a nested list, that bundles up the alternating positive and negative floats. Following is the python code I have written for this purpose.
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))
And following is the result:
[[(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)]]
**Now my question is: In the output it has not considered values having keys from 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}**
Can someone help me find out the error in my code?
CodePudding user response:
So the issue that you had with your code is that you are adding the final elements to list1 but there is not final appending of those elements to list3. You could add an check to see if there are any elements in either of list1 or list2 and add that as a final append to list3.
However, cleaning up your logic you could just go with something like this:
def is_positive(value): # returns a boolean; checking the positive / negative type of the float
return value > 0
final_list = []
for key, value in dict4.items():
# if there are no elements in final_list add a new inner list
if len(final_list) == 0 or (is_positive(final_list[-1][-1][1]) != is_positive(value)): # check if the previous element was negative or positive; if both of the elements are not the same then add a new list with this element
final_list.append([(key, value)])
else: # if the current positive/negative value is the same between the last and the current value add it to the last list
final_list[-1].append((key, value))
CodePudding user response:
Thank you @Andrew Ray, for helping me find the problem in my code. Also thank you for your code, that is very concise and more pythonic. Nonetheless, I have made slight changes in my own code and it does work now.
list1=[]
list2=[]
list3=[]
count1=0
count=0
for i, j in dict4.items():
if count1!=len(dict4)-1:
if j>0:
if len(list3)==count:
list1.append((i, j))
count1 =1
else:
list1.append((i, j))
list3.append(list(list2))
list2.clear()
count1 =1
count =2
elif j<0:
if len(list3)==count:
list3.append(list(list1))
list2.append((i, j))
list1.clear()
count1 =1
else:
list2.append((i, j))
count1 =1
else:
if len(list1)!=0:
list3.append(list(list1))
else:
list3.append(list(list2))