Join @@ Developer`PartitionMap[If[Length@# === 1, #, {#[[1]], {#[[2, 1]], #[[1, 2]]}}] &,
list1, 2, 1, 1, {}] == list2
True
Join @@ (If[Length@# === 1, #, {#[[1]], {#[[2, 1]], #[[1, 2]]}}] & /@
Partition[list1, 2, 1, 1, {}]) == list2
True
Steps:
Partition list
into parts of length up to 2 with offset 1 (take 2 elements move 1 step):
l1 = Partition[list1, 2, 1, 1, {}]
{{{-2.25, 24}, {-2.24, 0}},
{{-2.24, 0}, {-2.23, 0}},
{{-2.23, 0}, {-2.22, 10}},
{{-2.22, 10}, {-2.21, 32}},
{{-2.21, 32}, {-2.2, 50}},
{{-2.2, 50}}}
The function
func = If[Length@# === 1, #, {#[[1]], {#[[2, 1]], #[[1, 2]]}}] &;
leaves objects with Length
1 untouched, and, for two-element arguments modifies the second element to make its last entry equal to that of the first.
Applied to the first element of l1
it gives
func @ l1[[1]]
{{-2.25, 24}, {-2.24, 24}}
Mapped to each element of l1
it gives
func /@ l1
{{{-2.25, 24}, {-2.24, 24}},
{{-2.24, 0}, {-2.23, 0}},
{{-2.23, 0}, {-2.22, 0}},
{{-2.22, 10}, {-2.21, 10}},
{{-2.21, 32}, {-2.2, 32}},
{{-2.2, 50}}}
Flattening this gives list2
.
Developer`PartititionMap[func, list, other arguments]
is equivalent to func /@ Partition[list, other arguments]
, that is, it applies the function to each element of the partition.
Riffle[list1, Transpose[{Rest[#1], Most[#2]}] & @@ Transpose[list1]]
? $\endgroup$list2
. I think you want it to belist2 = {{-2.25, 24}, {-2.24, 24}, {-2.24, 0}, {-2.23, 0}, {-2.23, 0}, {-2.22, 0}, {-2.22, 10}, {-2.21, 10}, {-2.21, 32}, {-2.20, 32}, {-2.20, 50}}
$\endgroup$