6
$\begingroup$

Consider the following list:

{{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}}

I want to obtain the second element of a sub-list with respect to the first element of the sub-list.

for example: given 0.4 I would like to retrieve 6, given 0.1 I would like to retrieve 5.

The first element in the sub-lists are monotonically increasing and non repetitive.

$\endgroup$
2
  • 2
    $\begingroup$ Have you tried anything? $\endgroup$
    – Kuba
    Commented May 16, 2018 at 13:55
  • 1
    $\begingroup$ @Kuba, now there are many things to try and to learn from. best $\endgroup$
    – jarhead
    Commented May 16, 2018 at 15:17

7 Answers 7

7
$\begingroup$

Two possible examples (say for example you are interested in 0.1, but it works with any entry):

list = {{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}};
Cases[list, {0.1, _}]
Select[list, #[[1]] == 0.1 &]

Edit: if you only want the second element, go for:

Cases[list, {0.1, _}][[1, 2]]
Select[list, #[[1]] == 0.1 &][[1, 2]]
$\endgroup$
1
  • 5
    $\begingroup$ Consider also Cases[list, {0.1, b_} -> b] to return only the second element of each sublist. $\endgroup$
    – MarcoB
    Commented May 16, 2018 at 14:05
7
$\begingroup$

Another approach is to convert your List to an Association:

list = {{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}};
arse = AssociationThread @@ Transpose@list;
arse[0.4] (* 6 *)
arse[0.1] (* 5 *)

This assumes that each Key (each first element of a pair) are unique, but otherwise your question would be ill-posed.

$\endgroup$
4
  • $\begingroup$ Please, don't call associations that way... ;) $\endgroup$ Commented May 16, 2018 at 15:21
  • $\begingroup$ @HenrikSchumacher Aah, I was going to ask for explanations (like it's a bad habit to call "association[something]`). Then I got it :D I'll edit to raise the ambiguity ;) $\endgroup$
    – anderstood
    Commented May 16, 2018 at 15:29
  • $\begingroup$ Not sure wether that is better... =D $\endgroup$ Commented May 16, 2018 at 15:31
  • $\begingroup$ It raises the ambiguity! $\endgroup$
    – anderstood
    Commented May 16, 2018 at 15:32
5
$\begingroup$

Given that you only want the second element, and there are no repeats, you could use FirstCase

secondElement[firstElement_] := 
 FirstCase[{firstElement, other_} :> other]

secondElement[.1]@{{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}}
(* 5 *)

secondElement[0.4]@{{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}}
(* 6 *)
$\endgroup$
5
$\begingroup$
Pick [#[[All, 2]], #[[All, 1]], 0.4] &@lst

{6}

Pick [#[[All, 2]], #[[All, 1]], 0.1] &@lst

{5}

Pick [#[[All, 2]], #[[All, 1]], 0.1 | 0.4] &@lst 

{5, 6}

$\endgroup$
2
$\begingroup$
list = {{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}};

Create an Association

look = <|Rule @@@ list|>

<|0.1 -> 5, 0.3 -> 18, 0.4 -> 6, 0.7 -> 9, 1.1 -> 5|>

Query one value

look[0.4]

6

Use Lookup to query more than one value

Lookup[look, {0.1, 0.4}]

{5, 6}

Handling of missing keys

Lookup[look, {0.1, 0.4, -1}]

{5, 6, Missing["KeyAbsent", -1]}

Lookup[look, {0.1, 0.4, -1}, Nothing]

{5, 6}

Use KeyTake to get keys and values (missing keys are ignored)

KeyTake[{0.1, 0.4, -1}] @ look

<|0.1 -> 5, 0.4 -> 6|>

$\endgroup$
2
$\begingroup$
list = {{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}};

Using AssociatePairs by Taliesin Beynon

Associate = ResourceFunction["AssociatePairs"];

asc = Associate[list]

<|0.1 -> 5, 0.3 -> 18, 0.4 -> 6, 0.7 -> 9, 1.1 -> 5|>

asc[0.4]

6

Lookup[asc, {0.4, 0.1}]

{6, 5}

$\endgroup$
2
$\begingroup$
list = {{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}};

Using ReplaceList:

f[list_List, vals_?AtomQ | vals : {__?NumericQ}] := 
If[Length@# == 1, First@#, #] &@
ReplaceList[list, {___, s : {Alternatives @@ vals, b_}, ___} :> b]

f[list,0.5]

6

f[list, {0.4, 0.1}]

{6, 5}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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