I need a help. I want to chunk my list with float numbers into sublists to compare two decimal numbers to see if the difference between the consecutive numbers are in the range 10. To more specific, my expectation is to split them for example, those numbers from 70-80 to be in a list, from 80-90 to be in another list, and so on. Firstly, I sorted my list and then tried to compare two elements, but the result is different from that I want. Here is my attempt to compare numbers:
mylist = [125.90720268, 122.08697428, 86.70855817, 82.68482956, 75.99304643, 71.92440719, 80.92440719]
chunked_list = []
sort_list = sorted(mylist, key = lambda x:float(x))
curr = [sort_list[0]]
print("sort_list = ", sort_list)
for x in sort_list[1:]:
if abs(x - curr[-1]) < 10:
chunked_list.append(curr)
curr = [x]
else:
curr.append(x)
chunked_list.append(curr) ```
The result is:
[[71.92440719],
[75.99304643],
[80.92440719],
[82.68482956],
[86.70855817, 122.08697428],
[125.90720268]]
while I want to have this result:
[[71.92440719, 75.99304643], [80.92440719, 82.68482956, 86.70855817], [122.08697428, 125.90720268]]
Also, I tried to use math.isclose() function to compare two decimal numbers, but I failed again. I really appreciate that someone tells me where I make a mistake.
CodePudding user response:
You can use itertools.pairwise
(or its equivalent recipe if you're using Python of version < 3.10) to iterate through the list in adjacent pairs, and append to the output list a new sub-list if the difference between the pair is greater than 10 while keep appending to the last sub-list the current item:
from itertools import pairwise
output = []
for a, b in pairwise(sorted(mylist)):
if not output or b - a > 10:
output.append([] if output else [a])
output[-1].append(b)
Given your sample input, output
becomes:
[[71.92440719, 75.99304643, 80.92440719, 82.68482956, 86.70855817], [122.08697428, 125.90720268]]
Demo: https://replit.com/@blhsing/ImperturbableCrushingAdministrator
EDIT: Since you updated your question with a revised requirement that the numbers should be grouped in multiples of 10, you can append a new sub-list to the output list when the current item divided by 10 is different from the first item of the last sub-list divided by 10:
output = []
for i in sorted(mylist):
if not output or i // 10 != output[-1][0] // 10:
output.append([])
output[-1].append(i)
output
becomes:
[[71.92440719, 75.99304643], [80.92440719, 82.68482956, 86.70855817], [122.08697428, 125.90720268]]