Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trimming sublists

I have a list normalized_result_lists containing sublists. For each sublist, I want to identify the first 10 unique elements and trim upto that element. I present the current and expected outputs.

normalized_result_lists = [
    [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    1.0012468827930174, 1.0024937655860349, 1.0049875311720697, 1.0087281795511223,
    1.0112219451371571, 1.0187032418952617, 1.0224438902743143, 1.0286783042394014,
    1.0299251870324189, 2.0, 3.0],
    [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    1.0013812154696133, 1.0013812154696133, 1.0055248618784531, 1.0055248618784531,
    1.0055248618784531, 1.0096685082872927, 1.0138121546961325, 1.0179558011049723,
    1.020718232044199, 1.0248618784530388, 1.031767955801105, 1.0414364640883977,
    1.0524861878453038, 1.0607734806629834, 1.0662983425414365]
]

unique_elements = set()
trimmed_lists = []

for lst in normalized_result_lists:
    trimmed_list = []
    for elem in lst:
        trimmed_list.append(elem)
        unique_elements.add(elem)

        if len(unique_elements) == 10:
            break

    trimmed_lists.append(trimmed_list)

print(trimmed_lists)

The current output is

[[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0012468827930174, 1.0024937655860349, 1.0049875311720697, 1.0087281795511223, 1.0112219451371571, 1.0187032418952617, 1.0224438902743143, 1.0286783042394014, 1.0299251870324189], [1.0]]

The expected output is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0012468827930174, 1.0024937655860349, 1.0049875311720697, 1.0087281795511223, 1.0112219451371571, 1.0187032418952617, 1.0224438902743143, 1.0286783042394014, 1.0299251870324189], 
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    1.0013812154696133, 1.0013812154696133, 1.0055248618784531, 1.0055248618784531,
    1.0055248618784531, 1.0096685082872927, 1.0138121546961325, 1.0179558011049723,
    1.020718232044199]

>Solution :

You have to set unique_elements to an empty set after each iteration of loop so do this


normalized_result_lists = [
    [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    1.0012468827930174, 1.0024937655860349, 1.0049875311720697, 1.0087281795511223,
    1.0112219451371571, 1.0187032418952617, 1.0224438902743143, 1.0286783042394014,
    1.0299251870324189, 2.0, 3.0],
    [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    1.0013812154696133, 1.0013812154696133, 1.0055248618784531, 1.0055248618784531,
    1.0055248618784531, 1.0096685082872927, 1.0138121546961325, 1.0179558011049723,
    1.020718232044199, 1.0248618784530388, 1.031767955801105, 1.0414364640883977,
    1.0524861878453038, 1.0607734806629834, 1.0662983425414365]
]

trimmed_lists = []

for lst in normalized_result_lists:
    trimmed_list = []
    unique_elements = set() # For each iteration of outer loop setting unque element to empty set
    for elem in lst:
        trimmed_list.append(elem)
        unique_elements.add(elem)

        if len(unique_elements) == 10:
            break

    trimmed_lists.append(trimmed_list)

print(trimmed_lists)

Output

[[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 
1.0, 1.0, 1.0, 1.0012468827930174, 1.0024937655860349, 1.0049875311720697, 
1.0087281795511223, 1.0112219451371571, 1.0187032418952617, 
1.0224438902743143, 1.0286783042394014, 1.0299251870324189], [1.0, 1.0,
 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 
1.0013812154696133, 1.0013812154696133, 1.0055248618784531, 
1.0055248618784531, 1.0055248618784531, 1.0096685082872927, 
1.0138121546961325, 1.0179558011049723, 1.020718232044199, 
1.0248618784530388, 1.031767955801105, 1.0414364640883977]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading