5
$\begingroup$

If I have a list such as:

list={{90.,1.41813*10^-7},{90.,3.64722*10^-7},{89.9999,6.92632*10^-7},{89.9999,1.1241*10^-6},{89.9999,1.65807*10^-6},{89.9998,2.29371*10^-6},{89.9997,3.03034*10^-6},{89.9996,3.86738*10^-6},{89.9996,4.80431*10^-6},{89.9995,5.84068*10^-6},{89.9993,6.97609*10^-6},{89.9992,8.21018*10^-6},{89.9991,9.5426*10^-6},{89.999,0.000010973},{89.9988,0.0000125012},{89.9986,0.0000141269},{89.9985,0.0000158498},{89.9983,0.0000176696},{89.9981,0.0000195862},{89.9979,0.0000215994},{89.9977,0.0000237089},{89.9975,0.0000259145},{89.9972,0.0000282162},{89.997,0.0000306136},{89.9968,0.0000331066},{89.9965,0.0000356951},{89.9962,0.0000383789},{89.9959,0.0000411578},{89.9957,0.0000440317},{89.9954,0.0000470005},{89.995,0.0000500641},{89.9947,0.0000532222},{89.9944,0.0000564747},{89.9941,0.0000598216},{89.9937,0.0000632627}}

How can I find the value that corresponds to the the second position of one of the sublist given the value of the first position?. For example, inside the list I have {89.9983,0.0000176696} how can I find the number 0.0000176696 given the value of 89.9983 which is contained in the list.

EDIT: I tried using something like Select[list, MemberQ[89.9983,#] &][[2]] but it does not work

Thank you.

$\endgroup$
4
  • 1
    $\begingroup$ Try Select[list, SameQ[#[[1]], 89.9983] &] $\endgroup$ Commented May 21, 2020 at 2:57
  • 1
    $\begingroup$ If you are sure that the value is there, you could use 89.9983 /. Rule @@@ list. $\endgroup$ Commented May 21, 2020 at 3:00
  • $\begingroup$ @MarcoB Thank you! This works great!. You should put it as an answer too. I appreciate it. $\endgroup$ Commented May 21, 2020 at 3:02
  • $\begingroup$ You can use Position! $\endgroup$ Commented May 22, 2020 at 20:49

5 Answers 5

6
$\begingroup$

One way could be

num = 89.9983 ; Cases[list,{ num , x_} :> x] (* {0.0000176696} *) 
$\endgroup$
3
  • $\begingroup$ Thank you Nasser!. This work great. However, is it possible to obtain the value without {}?. I tried Flatten[Cases[lista, { 89.9983, x_} :> x]] but it is still with the {}. Thanks $\endgroup$ Commented May 21, 2020 at 3:00
  • 4
    $\begingroup$ @John Use FirstCase instead of Cases or First@Cases[...]. $\endgroup$ Commented May 21, 2020 at 3:02
  • $\begingroup$ Slightly modifying this with OrderlessPatternSequence makes this really powerful, Nasser. Check out my answer, it leads me to believe there is an ideal arrangement for a method like this!! $\endgroup$ Commented May 23, 2020 at 17:26
4
$\begingroup$
data//Pick[#[[All,2]], #[[All,1]], 89.9983]& 

{0.0000176696}

%[[1]] 

0.0000176696

If the use of Select is required, maybe:

Select[list, #[[1]]==89.9983 &][[1,2]] 

0.0000176696

(But IMO Pick is a very powerful command in situations like this)

$\endgroup$
1
  • $\begingroup$ A 'just for fun' Reap/Sow solution: Reap[Sow[#2, #===89.9983]&@@@list,True][[2,1,1]] $\endgroup$ Commented May 22, 2020 at 10:19
2
$\begingroup$

I’ve been really into using Extract lately. So, here’s what I would do:

 Extract[Position[list,{OrderlessPatternSequence[89.9983,___]}]][list][[-1,-1]] (* 0.0000176696 *) 

If you know the underlying list structure, that is why we use [[-1,-1]]. If we don’t know the value, or, perhaps we only want to write one finder function, we can do the following:

 FirstCase[list,{OrderlessPatternSequence[89.9983,v___]}:>v] (* 0.0000176696 *) 

Further generalization gives us:

 valueCorrepondingTo[list_,num_]:=FirstCase[list,{OrderlessPatternSequence[num,v___]}:>v]; valueCorrepondingTo[list, 0.0000176696] (* 89.9983 *) 

I think this is really cool, this type of functionality and it is completely thanks to OrderlessPatternSequence.

$\endgroup$
2
$\begingroup$

Using Lookup:

Clear["Global`*"]; list = {{90., 1.41813*10^-7}, {90., 3.64722*10^-7}, {89.9999, 6.92632*10^-7}, {89.9999, 1.1241*10^-6}, {89.9999, 1.65807*10^-6}, {89.9998, 2.29371*10^-6}, {89.9997, 3.03034*10^-6}, {89.9996, 3.86738*10^-6}, {89.9996, 4.80431*10^-6}, {89.9995, 5.84068*10^-6}, {89.9993, 6.97609*10^-6}, {89.9992, 8.21018*10^-6}, {89.9991, 9.5426*10^-6}, {89.999, 0.000010973}, {89.9988, 0.0000125012}, {89.9986, 0.0000141269}, {89.9985, 0.0000158498}, {89.9983, 0.0000176696}, {89.9981, 0.0000195862}, {89.9979, 0.0000215994}, {89.9977, 0.0000237089}, {89.9975, 0.0000259145}, {89.9972, 0.0000282162}, {89.997, 0.0000306136}, {89.9968, 0.0000331066}, {89.9965, 0.0000356951}, {89.9962, 0.0000383789}, {89.9959, 0.0000411578}, {89.9957, 0.0000440317}, {89.9954, 0.0000470005}, {89.995, 0.0000500641}, {89.9947, 0.0000532222}, {89.9944, 0.0000564747}, {89.9941, 0.0000598216}, {89.9937, 0.0000632627}}; lut = First@# -> Last@# & /@ list; Lookup[lut, 89.9983] 

0.0000176696


There is an optional default third argument (without which a Missing message is generated for non-existent keys).

Lookup[lut, 10, "Absent"] 

"Absent"

$\endgroup$
2
$\begingroup$

Using Merge and MapApply (new in 13.1)

a = Merge[# &] @ MapApply[Rule] @ list; 

Query one key with one value

a[89.9983] 

{0.0000176696}

Query one key with several values

a[90.] 

{1.41813 * 10^-7, 3.64722 * 10^-7}

Query more than one key with Lookup

Lookup[a, {90., 89.9983, -1}] 

{{1.41813 * 10^-7, 3.64722 * 10^-7},
{0.0000176696},
Missing["KeyAbsent", -1]}

If we also want to see the keys:

KeyTake[{90., 89.9983}] @ a 

<|
90. -> {1.41813 * 10^-7, 3.64722 * 10^-7},
89.9983 -> {0.0000176696}
|>

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.