2

So I have a list like so

list={{a},{b,c},{d,e,f},{g,h}}

and the basic logic I want is to be able to manipulate each of the nested lists individually.

So If I wanted to take the average of each list I would do something like

avglist=Table[(1/3) Sum[list[[n,j]],{j,3}],{n,4}]

But the problem with this is when the program would call list[[1,2]]

So the gist is I am trying to write a program that could average/in general manipulate each of the nested lists of uneven lengths without having to know how many entries are in the list with the most entries.

I can't just add zeroes into the nested lists with fewer entries because if I want to manipulate them first then average it would affect the average.

Thanks

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
MAKM
  • 23
  • 3

1 Answers1

1

If in general you want to "do the same thing to each element of a list" then think Map.

list = {{a}, {b, c}, {d, e, f}, {g, h}};
Map[Mean, list]

(* Out[2]= {a, (b + c)/2, 1/3 (d + e + f), (g + h)/2} *)

Replace Mean with a function of your own definition if necessary.

myF[v_] := v.v + Length[v] - First[v];
Map[myF, list]

(* Out[4]= {1 - a + a^2, 2 - b + b^2 + c^2, 3 - d + d^2 + e^2 + f^2, 2 - g + g^2 + h^2} *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
Bill
  • 12,001
  • 12
  • 13