S
S
Sergey Yavin2022-02-01 10:16:55
Python
Sergey Yavin, 2022-02-01 10:16:55

How to sort through the numbers if they reduced the fraction?

Good morning everyone, due to insufficient knowledge in the field of mathematics, I can not solve the problem.
Given an array of arrays. The function needs to reduce the array with the value of the input parameter t as follows.

mass = [
[34.224, 51],
[34.226, 1],
[34.227, 32],
[34.234, 521],
[34.236, 5],
[34.247, 24],
[34.248, 334],
[34.274, 1] ,
[34.274, 22],
[34.290, 321],
[34.292, 51],
[34.299, 34]
]

t = 0.01 Iterate
over the array and reduce mass[i][0] to hundredths without rounding, then add the same values.
The result should be like this:
new_mass = [
[34.22, 84],
[34.23, 526],
[34.24, 358],
[34.27, 23],
[34.29, 406],
]

Also at t = 0.1, the function reduced to tenths.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Grigoriev, 2022-02-01
@sjaserds

apparently like knowledge of python

from collections import defaultdict

t = 0.01
mass = [
    [34.224, 51],
    [34.226, 1],
    [34.227, 32],
    [34.234, 521],
    [34.236, 5],
    [34.247, 24],
    [34.248, 334],
    [34.274, 1],
    [34.274, 22],
    [34.290, 321],
    [34.292, 51],
    [34.299, 34]
]

d = defaultdict(int)

for key, value in mass:
    d[int(key / t)] += value

print([[f'{k * t:.2f}', v] for k, v in d.items()])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question