Group Abstract Group Abstract

Message Boards Message Boards

1
|
6.5K Views
|
3 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Number items in a list of lists, Functional programming alternative?

Posted 9 years ago
Attachments:
POSTED BY: Robert McHugh
3 Replies
Posted 9 years ago

Thanks for answering. 9 lines of code down to 2.
So much to learn about using this tool properly!

POSTED BY: Robert McHugh
vec1 = {a, b, c, d};
vec2 = {a, b, c, d, e, f}; 
vec3 = {a, b, c};  
f[v1_, v2_, v3_] := 
 Block[{i = 1}, Map[Map[{i++, #} &, #] &, {v1, v2, v3}]]
f[vec1, vec2, vec3]

out: {{{1, a}, {2, b}, {3, c}, {4, d}}, {{5, a}, {6, b}, {7, c}, {8, d}, {9, e}, {10, f}}, {{11, a}, {12, b}, {13, c}}}

POSTED BY: l van Veen

You could use:

i = 0;
Map[{++i, #} &, allVecs, {2}]

or

i = 0;
Replace[allVecs, x_ :> {++i, x}, {2}]

or

Internal`PartitionRagged[MapIndexed[{#2[[1]], #1} &, Join @@ allVecs], Length /@ allVecs]

or

tmp = Join @@ allVecs
tmp = Transpose[{Range[Length[tmp]], tmp}];
Internal`PartitionRagged[tmp, Length /@ allVecs]

or

tmp = Join @@ allVecs
tmp = Transpose[{Range[Length[tmp]], tmp}];
lens = Length /@ allVecs;
begins = Accumulate[Prepend[Most[lens], 1]];
ends = Accumulate[lens];
MapThread[tmp[[#1 ;; #2]] &, {begins, ends}]

or

pos = Position[allVecs, _, {2}, Heads -> False]
numbers = Range[Length[pos]]
replacements = MapThread[#1 -> {#2, Extract[allVecs, #1]} &, {pos, numbers}]
ReplacePart[allVecs, replacements]
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard