Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Extracting information from lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63367] Re: Extracting information from lists
  • From: rudy <rud-x at caramail.com>
  • Date: Tue, 27 Dec 2005 04:42:38 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Hello,

li = {7, 9, 1, 6, 8, 1, 1, 6, 5, 1, 1, 1, 8, 7, 6, 1, 1, 1, 1, 7};

lolo = {0, 2, 1, 1, 0, 1, 1, 0, 0, 2, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0};

lulu = {a, b, d, d, d, d, c, f, g, t, d, d, e, r, d, d, d, e, r, d, d, e, f, r,f};

Here are three function with different degrees of generalisation:

(in the 3 functions, the parameter n is the max length of the series of 1 considered)

this one answer exactly to your question:

fct1[li_, n_] := Module[{inter, res, pourN},

pourN[l_List, i_Integer] := Array[Position[Split[l], 
Table[1, {#}]] &, {i}] // Flatten;

inter = pourN[li, n];

res = (Length[#] + 1 &) /@ ((Take[Split[li], # - 1] // Flatten) & /@ 
inter) 
];

example:
in > fct1[li, 5]
out > {3, 6, 10, 16}

but some problems arise when there are some series of '1' of same length in the list and when these series are in disorder (example in the list lolo):

in > fct1[lolo, 5]
out > {3, 6, 12, 19, 28, 15, 22}

when this result is given, whe know the position of the series but we ignore the length of each one...



this one answer your question in giving:
{the number of 1 in the serie, the position of the serie}


fct2[lis_, n_] := Module[{inter1, inter2, res, pourN},

pourN[l_List, i_Integer] := Array[({Position[Split[l], Table[
1, {#}]], #} // Flatten) &, {i}];

inter1 = pourN[lis, n] // Select[#, Length[#] >= 2 &] &;

inter2 = Table[{#[[i]], Last[#]}, {i, 1,
Length[#] - 1}] & /@ inter1 // Flatten[#, 1] &;

res = {#[[2]], Length[#[[1]]] + 1} & /@ ({Take[Split[
lis], #[[1]] - 1] // Flatten, #[[2]]} & /@ inter2)

];

for example:

in > fct2[li, 5]
out > {{1, 3}, {2, 6}, {3, 10}, {4, 16}}


this one is the more general: it gives the positions of all the series of character 's' with max length 'n' in the list with their positions:

fct3[lis_, s_, n_] := Module[{inter1, inter2, res, pourN},

pourN[l_List, i_Integer] := Array[({Position[Split[l], Table[
s, {#}]], #} // Flatten) &, {i}];

inter1 = pourN[lis, n] // Select[#, Length[#] >= 2 &] &;

inter2 = Table[{#[[i]], Last[#]}, {i, 1, Length[#] - 1}] & /@ inter1 // 
Flatten[#, 1] &;

res = {#[[2]], Length[#[[
1]]] + 1} & /@ ({Take[Split[
lis], #[[1]] - 1] // Flatten, #[[2]]} & /@ inter2)

];


for example:

in > fct3[lulu, d, 5]
out > {{2, 11}, {2, 20}, {3, 15}, {4, 3}}

in > fct3[lulu, d, 2]
out > {{2, 11}, {2, 20}}


regards
Rudy


  • Prev by Date: Lightweight Structs - Practical Example (Data Types in Mathematica)
  • Next by Date: Re: Re: Questions regarding MatrixExp, and its usage
  • Previous by thread: Re: Extracting information from lists
  • Next by thread: Re: Extracting information from lists