[GAP Forum] Need help with lists

Rudolf Zlabinger Rudolf.Zlabinger at chello.at
Mon Jun 11 20:03:49 BST 2007


Dear GAP Forum,

On 11 Jun 2007, at 14:45, muniru asiru wrote:

> I need help.
>
> While working with Gap, I came accross the following
> list of list of list with 10,3 and 7 items
> respectively.  My interest is to REPLACE (please note)
> this list of list of list (a5, below) with a list of
> list so that I will have 30 elements made up of 7
> elements in each list of list.
>
>
> Thank you

There is a more general solution using my function "Array" (attached). That 
means, thereby you can reformat deliberately your list structure:

a5:=[ [ [ ... ] ] ];
a307:=Array([30,7]);

a210:=Flat(a5);

k:=0;
for i in [1..30]    do
 for j in [1..7] do
  k:=k+1;
  a307[i][j]:=a210[k];
 od; #j
od;  #i

gap> Length(a307);
30
gap> Length(a307[1]);
7
gap> a210test:=Flat(a307);;
gap> a210=a210test;
true
gap>

all the best, Rudolf Zlabinger
-------------- next part --------------
Array:=function(bounds)

# creates an array of empty lists of arbitrary nesting levels (dimensions)
# and bounds for each level
# bounds is a list of positive integers determining the length of each dimension
# the length of bounds is the dimension of the array

# at element level the dimension of the array is 1 higher, 
# as the last level are also empty lists

local array,IterateArray;

IterateArray:=function(array,bounds) 

# iterative call for decreasing levels of array and bounds

local i,dim,boundsrecall;

# i.. index, 
# dim the recent level of bounds, 
# boundsrecall.. shortened bounds for recalls

dim:=Length(bounds);                   # the varying length of bounds

for i in [1..bounds[1]] do              # 1 recallcascade for one add

  Add(array,[]);                         # enlarging the recent level of array
 
  if dim>1 then                          # dim=1 is the last level

    boundsrecall:=bounds{[2..dim]};        # shorten bounds for next level  
    IterateArray(array[i],boundsrecall);   # recursion for the next lower level of array   

  fi;                                    # end of dimension > 1 clause

od;                                    # end of bound loop

return array;
end;


array:=[];


array:=IterateArray(array,bounds);      # starting call of add cascade 


return array;
end;


More information about the Forum mailing list