Creating Lists From Loops

Consider the following:

lists = Import["G:/Notebooks/Lists.csv"];

col7 = lists [[All, 7]];

For[i=0,i<101,i++,Print[Count[col7,i]]]


This will get a 100 cells printed out but what I really want to do is capture this output to another variable as a list. So far the best way I have figured out is to copy the output, edit the text, and manually paste it back in to a variable. There surely has to be a better way?

-
Is this a duplicate of mathematica.stackexchange.com/questions/2930/… , asked just an hour or so ago? – Szabolcs Mar 14 '12 at 15:24
It has some similarity. In one example, AppendTo was used. I have tried AppendTo, and it doesn't work. I am also not dealing with a table, just want a one dimensional list of data. I have tried about 5 different ways I could think of to accomplish this using the list manipulation commands and I cannot seem to coax mma to do this. – Sinistar Mar 14 '12 at 15:41
I really don't understand why Table doesn't work for you, but maybe I'm just too tired. Can you explain? – Szabolcs Mar 14 '12 at 15:44
@Sinistar: how does the AppendTo not work? Did you define the variable (such as col7 = {}) before using it in AppendTo? Note that AppendTo[col7,someList] is the same as col7 = Append[col7,someList] but both require col7 in this case to be defined – tkott Mar 14 '12 at 17:21
@tkott It didn't work because you used the wrong brackets. I've edited your answer in the other post. – Sjoerd C. de Vries Mar 14 '12 at 18:26

Generally, the solution to this kind of problems is using Table:

Table[Count[col7, i], {i, 100}]


However, in this case I suggest Tally or BinCounts:

Tally[col7]

BinCounts[col7, {0, 100, 1}]


Tally will not list elements that don't appear at all.

-
Seems to be effective... – Sinistar Mar 14 '12 at 15:53

In addition Array and Map work. Thanks Mr.Wizard. There are some subtle differences in their arguments the documentation does a good job of explaining them.

I assume you were looking for counts of the occurrences of values 0 - 100 in col7.

min = 0;
max = 9; (* In your example it would be 100 *)
col7 = RandomInteger[{min, max}, max*10];

a = {}; For[i = min, i <= max, i++, AppendTo[a, Count[col7, i]]]; a

Array[Count[col7, #] &, max + 1 - min, min]

Map[Count[col7, #] &, Range[min, max]]

Table[Count[col7, i], {i, min, max}]

Sort[Tally[col7], #1[[1]] < #2[[1]] &][[All, 2]]

BinCounts[col7, {min, max + 1, 1}]


Results:

{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}
{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}
{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}
{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}
{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}
{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}


The numbers will vary because of how I generated col7, but they should be the same for all six examples.

Performance results:

min = 0;
max = 100;
col7 = RandomInteger[{min, max}, 10000];


-
Nice comparison. The custom sort #1[[1]] < #2[[1]] & is not needed (and slower) because the default sort function will produce the same result in this case. Also, it is faster to use SortBy to sort by the first element, if you need to do that explicitly: SortBy[. . ., First] – Mr.Wizard Mar 15 '12 at 10:44