Python function problem

Hi! I am trying to create a function that grouping the values from the dictionary in a list, by comparing a value with other values in from the list.

Condition: The difference between the first value and second value must be <=50, to be categorized in the same list.

Given input as:

sample_objects = [
    {
        'id': 0,
        'value': 10
    },
    {
        'id': 1,
        'value': 20
    },
    {
        'id': 2,
        'value': 80
    },
    {
        'id': 3,
        'value': 115
    },
    {
        'id': 4,
        'value': 130
    },
    {
        'id': 5,
        'value':135
    },
    {
        'id': 6,
        'value':250
    },
    {
        'id': 7,
        'value':280
    }
]

The output should be:

sample_objects = [
    [
        {
            'id': 0,
            'value': 10
        },
        {
            'id': 1,
            'value': 20
        }
    ],
    [
        {
            'id': 2,
            'value': 80
        },
        {
            'id': 3,
            'value': 115
        },
        {
            'id': 4,
            'value': 130
        },
    ],
    [
        {
            'id': 5,
            'value':135
        }
    ],
    [
        {
            'id': 6,
            'value':250
        },
        {
            'id': 7,
            'value':280
        }
    ]
]

My current function:

def layout_processing():
    output = []
    for y in range(len(sample_objects)):
        test=[]
        for z in range(y+1, len(sample_objects)):
                if abs(sample_objects[y]['value'] - sample_objects[z]['value']) <= 50:
                    print(z, 'is in the same container with ', y, '\n')
                    test.append(sample_objects[y]['value'])
                    test.append(sample_objects[z]['value'])
                else:
                    print(z, 'is not in the same container with ', y, '\n')
        output.append(test)
    print('Test', output, '\n')
    return output

I hope someone can help with this! Thank you.

Do you have a specific question? At what point are you? What works? What doesn’t? Why do you think that is?

I do not have a specific question, because this is one of the functions that is needed in my project for threshold validation purpose.

I am trying to compare the values from the dictionary, if the difference between the values with other values are less than or equal to 50, it should categorized in the same list.

Below is the output for my current function:

I’m trying to figure out with what exactly you need help, that’s still not clear. Being as precise as possible, and giving more context will allow to help you easier. Otherwise somebody will just make guesses what is giving you troubles.

Therefore - what’s the problem? Is there even a problem, or you just have some general doubt? Is that output correct? If not, why not, how would look the correct result instead? What is working, what is not working?

My apologies for the unclear statements. Perhaps, I can explain by giving a simpler scenario.

Example:

  • The first value is 10. So, it should compare with other values (such as 20, 80, 115, 130, 135, 250, 280)

If the difference between the numbers (e:g: 20 - 10) is <= 50, it should be in the same list, so [[10,20]].
eg: 80 - 10, is more than 50, so it should be starting a new list, so [[10,20], [80]].
Then keep on comparing until all the values are in the list.

My current problem is that the output is not correct.
If input = [10, 20, 80, 115, 130, 135, 250, 280]
correct output should be: [[10, 20], [80, 115, 130], [135], [250, 280]]

(post deleted by author)

Thanks for update.

There might be few problems that’s causing incorrect output, but try focusing just at one at the time. Some issues can be exposing themselves in multiple places.

I’d start from figuring out how the empty list is added to the output.

Thank you for your help!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.