I have a list of ordered pairs produced by evaluating

{IsotopeData["Thorium226", "ExcitedStateEnergies"],
 IsotopeData["Thorium226", "ExcitedStateSpins"]}

I'm trying to remove all ordered pairs in the list of ordered pairs when the excited state spins is odd number. I am getting a empty list. Please help me.

  • $\begingroup$ Transpose@Cases[Transpose@out, {_, x_ /; ! OddQ@x}] where out is your list. $\endgroup$
    – ciao
    Aug 29, 2015 at 22:43

4 Answers 4

data = {IsotopeData["Thorium226", "ExcitedStateEnergies"], 
    IsotopeData["Thorium226", "ExcitedStateSpins"]} // Transpose;

(dataEven = Cases[data, {_, _?EvenQ}]) // Column

enter image description here

Some other alternatives

dataEven ==
 Select[data, EvenQ[#[[2]]] &] ==
 DeleteCases[data, {_, _?(! EvenQ[#] &)}] ==
 Pick[data, EvenQ[data[[All, 2]]]] ==
 GatherBy[data, EvenQ[#[[2]]] &][[1]] ==
 Gather[data, And @@ EvenQ[{#1[[2]], #2[[2]]}] &][[1]]

(* True *)

There are many ways to do this, e.g.:

dat = Transpose@data;
Cases[dat, {_, _?EvenQ}]
Select[dat, EvenQ[Last@#] &]
Pick[dat, EvenQ@Last@# & /@ dat]
Extract[dat, Position[dat, {_, _?EvenQ}]]

All yield:

{Quantity[72.20, "Kiloelectronvolts"], 2}, {Quantity[226.43, "Kiloelectronvolts"], 4}, {Quantity[447.3, "Kiloelectronvolts"],
6}, {Quantity[721.9, "Kiloelectronvolts"], 8}, {Quantity[805.2, "Kiloelectronvolts"], 0}, {Quantity[847.8, "Kiloelectronvolts"],
2}, {Quantity[1040.3, "Kiloelectronvolts"], 10}, {Quantity[1395.2, "Kiloelectronvolts"], 12}, {Quantity[1781.5, "Kiloelectronvolts"], 14}, {Quantity[2195.8, "Kiloelectronvolts"], 16}, {Quantity[2635.1, "Kiloelectronvolts"], 18}, {Quantity[3097.1, "Kiloelectronvolts"], 20}}


Your expression doesn't actually produce a list of ordered pairs, but rather two lists, one of excited spin energies and the other of excited spin states. Further, some the excited spin state values are missing and marked by Missing["NotAvailable"]. Presuming you not only wish to remove odd spins but missing ones too, I precede like so:

data = {ese, ess} = 
  {IsotopeData["Thorium226", "ExcitedStateEnergies"], 
   IsotopeData["Thorium226", "ExcitedStateSpins"]};
Extract[#, Position[ess, _?EvenQ]] & /@ data // Transpose
{{72.2`4.keV, 2}, {226.43`5.keV, 4}, {447.3`4.keV, 6}, {721.9`4.keV, 8},
 {805.2`4.keV, 0}, {847.8`4.keV, 2}, {1040.3`5.keV, 10}, {1395.2`5.keV, 12},
 {1781.5`5.keV, 14}, {2195.8`5.keV, 16}, {2635.1`5.keV, 18}, {3097.1`5.keV, 20}}

There is one important method missing in the previous answers and which consists in replacing subparts of a list with (replacement) rules (see for example this official doc).

By the way, you can also retrieve your data with this shorter syntax:

data=IsotopeData["Thorium226"][{"ExcitedStateEnergies", "ExcitedStateSpins"}]


(for versions<10.2 replace Nothing with Sequence[])

Thread@data /. {_, spinState_?OddQ} -> Nothing

enter image description here

Every pair in the Thread@data list whose second element (spinState) is odd is replaced with Nothing. As you can see, in the data I retrieved from the server there are missing spin state values (it seems the problem only concerns me ... ?) which have not been removed with this first replacement.

Actually, it gives the good opportunity to show how to easily chain multiple replacements to filter data:

Thread@data /. {_, spinState_?OddQ} -> Nothing /. {_, Missing[_]} -> Nothing

enter image description here


Your Answer

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

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