Filter Python list to remove similar values -
so have list around 100 float values between 0 , 1 , like:
[0.642918256477056, 0.6429182558639024, 0.6429182466700671, 0.6429182068585415, 0.6429180997884363, 0.6428178743476422, 0.6428174651415239, 0.6428167927406518, ... ]
what need way create more simple list, values same during 4 decimals removed , keep first. example: number 0,6429... repeated several times in original list, need once.
after still need know values (indexes) removed can "remap" them. example:
len(original_list) # result: 100 | remap 0:1 range (index0 0, index 100 1) len(filterted_list) # result: 10
for example, if index 5 of filteres_list index 80 in original_list, need know original remapped value 0.8.
sorry if can't explain better, if me first part help. thank guys.
use set , round:
li=[0.642918256477056, 0.6429182558639024, 0.6429182466700671, 0.6429182068585415, 0.6429180997884363, 0.6428178743476422, 0.6428174651415239, 0.6428167927406518] seen=set() data=[] n in li: nr=round(n,4) if nr not in seen: seen.add(nr) data.append(nr) # append n if want unrounded value print data # [0.6429, 0.6428]
you can either append data
either n
(unrounded) or nr
rounded value. order maintained.
Comments
Post a Comment