I have a program that handles big sets of data. Most of it runs really quickly except a few lines of the code that seem to sometimes take up to around a minute.
What these lines of code do is they take a list of 1 to 1000 different values ranging from 0 to 1000000. Then for a defined k number of times they filter the list and removes the value that is the most represented in that list (if multiple values are represented the same amount of times it just takes a random one).
Is there a way to write this so it takes less time to execute.
I run Windows 10 Pro with an Intel I7-8086K GTX1660 and 16Gb DDR4 RAM.
k = int(new1[1])
def most_frequent(lists):
return max(set(lists), key=lists.count)
ite = 0
while ite < k:
new = list(filter(lambda a: a != most_frequent(new), new))
ite += 1
As said before I am getting the results I want it's just that the program is extremely slow.
most_frequentfor each filtering operation, you should do this:most_frequent_element = most_frequent(new)Then use that value in filterlist(filter(lambda a: a != most_frequent_element , new))You should see a big difference