Home > front end >  Reduce python list size while preserving information
Reduce python list size while preserving information

Time:07-14

I have multiple long lists in my program. Each list has approximately 3000 float values. And there are around 100 such lists.

I want to reduce the size of each list to say, 500, while preserving the information in the original list. I know that it is not possible to completely preserve the information, but I would like to have the elements in the original list to have contribution to the values of the smaller list.

Let's say we have the following list, and want to shorten it to a lists of size 3 or 4.

myList = [[4.3, 2.3, 5.1, 6.4, 3.2, 7.7, 1.5, 6.5, 7.4, 4.1],
          [7.3, 3.5, 6.2, 7.4, 2.6, 3.7, 2.6, 7.1, 3.4, 7.1],
          [4.7, 2.6, 5.6, 7.4, 3.7, 7.7, 3.5, 6.5, 7.2, 4.1],
          [7.3, 7.3, 4.1, 6.6, 2.2, 3.9, 1.6, 3.0, 2.3, 4.6],
          [4.7, 2.3, 5.7, 6.4, 3.4, 6.8, 7.2, 6.9, 8.4, 7.1]]

Is there some way to do this. Maybe by averaging of some sort (?)

CodePudding user response:

You can do something like this:

from statistics import mean, stdev

myList = [[4.3, 2.3, 5.1, 6.4, 3.2, 7.7, 1.5, 6.5, 7.4, 4.1], [2.3, 6.4, 3.2, 7.7, 1.5, 6.5, 7.4, 4.1]]

shorten_list = [[max(i)-min(i), mean(i), round(stdev(i), 5)] for i in myList]

You can also include information such as the sum of the list or the mode. If you just want to take the mean of each list within your list, you can just do this:

from statistics import mean

mean_list = list(map(mean, myList))

CodePudding user response:

batching may work. I request you to add some data examples, to know whether it's a nested list or a normal list. I request you to look at this question

How do I split a list into equally-sized chunks?

this converts the list into equal batches

  • Related