Issue
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
[[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]]
Answered By - codester_09 aka Sharim
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.