Quantcast
Channel: Adding elements to some sublists of unequal length - Mathematica Stack Exchange
Browsing index pages (7 articles)

Answer by eldo for Adding elements to some sublists of unequal length

From the Association universe:KeyValueMap[Flatten @* List] @ Merge[# &] @ Map[GroupBy[First -> Rest], {a, b}]{{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}OrKeyValueMap[Flatten @*...

View Article


Answer by Syed for Adding elements to some sublists of unequal length

Clear["Global`*"];a = {{1, 3, 5, 2}, {2, 6, 2}, {3, 5, 6, 1, 2}, {4, 2}};b = {{2, 1}, {4, 3}};c = Join[b, Array[{#, x} &, Length@a]] // DeleteDuplicatesBy[First] // SortBy[First]{{1, x}, {2, 1},...

View Article


Answer by E. Chan-López for Adding elements to some sublists of unequal length

Another method using GroupBy:Values[GroupBy[Catenate[{a, b}], First, If[Length[#] == 2, Sequence @@@ {#[[1, All]], #[[2]][[2;;]]}, #[[1]]] &]](*{{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2,...

View Article

Adding elements to some sublists of unequal length

Complicated title for a simple problem: I have list a = {{1, 3, 5, 2}, {2, 6, 2}, {3, 5, 6, 1, 2}, {4, 2}} In which the first element of each sublist is basically an index. The following values are...

View Article

Answer by Vitaliy Kaurov for Adding elements to some sublists of unequal length

Perhaps something like this: ReplacePart[a, #1 -> Append[a[[#1]], #2]& @@@ b] If data are large and speedup is needed you might want to try Join ReplacePart[a, #1 -> Join[a[[#1]], {#2}] &...

View Article


Answer by Suba Thomas for Adding elements to some sublists of unequal length

Fold[Insert[#1, Last[#2], {First[#2], -1}] &, a, b]

View Article

Answer by kglr for Adding elements to some sublists of unequal length

You can make b into a list of rules rules = {#, a__} :> {#, a, #2} & @@@ b; and use it with ReplaceAll: a /. rules {{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}} or with Replace:...

View Article
Browsing index pages (7 articles)


Latest Images