0
$\begingroup$

Here's a following list:

L={{a,b},{c,d},{e,f}}. I want a function such that it sums a number over the first element of each pair. For instance, take the number 1. I need:

L={{a+1,b}, {c+1,d}, {e+1,f}}. Which function does this?

Thanks!

$\endgroup$
3
  • $\begingroup$ I'm sure this is a dupe, but: TranslationTransform[{1, 0}][{{a,b},{c,d},{e,f}}] $\endgroup$ Mar 12, 2018 at 16:13
  • 1
    $\begingroup$ No function necessary: L[[All, 1]] = L[[All, 1]] + 1. $\endgroup$
    – JimB
    Mar 12, 2018 at 16:28
  • $\begingroup$ L + ConstantArray[{1, 0}, Length[L]] $\endgroup$
    – bill s
    Mar 12, 2018 at 18:09

2 Answers 2

0
$\begingroup$
Clear[a, b, c, d, e, f]
lst = {{a, b}, {c, d}, {e, f}}
"TranslationTransform (JM's soln):"
TranslationTransform[{1, 0}]@lst
"ReplaceAll:"
lst /. {{x_, y_} -> {x + 1, y}}
"The ever reliable Map:"
{1 + First@#, Last@#} & /@ lst
"ReplacePart:"
ReplacePart[lst, xy : {_, 1} :> Extract[lst, xy] + 1]
"Part (JimB's answer); changes `lst`!:"
lst[[All, 1]] += 1  (* this is not the answer *)
lst  (* this is *)
$\endgroup$
0
$\begingroup$
l = {{a,b},{c,d},{e,f}}
Map[{#[[1]]+1,#[[2]]}&,l]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.