4
$\begingroup$

I have a list (Table), mostly composed of real numbers. The list also contains some non-real elements. The list is similar to the following (but much much larger):

{{0.11011887269, 0.02087291466, 0.02082444233, 0.02083704779, 
  0.02084940959, 0.02086181292, 0.02087425501, 0.02088673503, 
  0.02089925331, 0.02091181001, 0.02092440529, 0.02093703933, 
  0.02094971232, 0.02096242441, test1[18, 15]}, {0.10941526373, 
  0.017604005802, 0.017549654594, 0.017558686829, test1[19, 5], 
  test1[19, 6], test1[19, 7], test1[19, 8], test1[19, 9], 
  test1[19, 10], test1[19, 11], test1[19, 12], test1[19, 13], 
  test1[19, 14], test1[19, 15]}, {0.10890647143, 0.014986243752, 
  0.014927392616, 0.014934004368, test1[20, 5], test1[20, 6], 
  test1[20, 7], test1[20, 8], test1[20, 9], test1[20, 10], 
  test1[20, 11], test1[20, 12], test1[20, 13], test1[20, 14], 
  test1[20, 15]}}

I would like to construct a single row or column list that preserves the last real number element of each row of the above list. For example, with the above table, the resulting list should contain only 3 elements as:

{0.02096242441, 0.017558686829, 0.014934004368}

How can this resulting list be automatically constructed from the original one?

$\endgroup$
2
  • 2
    $\begingroup$ How about Table[Last[Cases[row, _Real]], {row, d}] where d is your list $\endgroup$
    – MelaGo
    Commented Apr 19, 2021 at 1:47
  • $\begingroup$ @MelaGo This is exactly what I needed. Thanks. $\endgroup$
    – Meclassic
    Commented Apr 19, 2021 at 1:59

4 Answers 4

2
$\begingroup$

With table your list of lists:

Cases[table, {___, a_?NumericQ, Except[_?NumericQ] ...} :> a, All]
{0.0209624, 0.0175587, 0.014934}
$\endgroup$
2
$\begingroup$
SequenceCases[list, {{__, a_Real, __test1}} -> a]

{0.0209624, 0.0175587, 0.014934}

$\endgroup$
2
$\begingroup$

Using SplitBy:

Last@#1 & @@@ (SplitBy[#, Head] & /@ list)

(*{0.0209624, 0.0175587, 0.014934}*)

Or using ReplaceList:

ReplaceList[list, {___, s : {__, a_Real, __test1}, ___} :> a]

(*{0.0209624, 0.0175587, 0.014934}*)
$\endgroup$
2
$\begingroup$

Using Cases:

For each entry in list reversed, the Cases command can pick the first instance of _Real:

list = {{0.11011887269, 0.02087291466, 0.02082444233, 0.02083704779, 
    0.02084940959, 0.02086181292, 0.02087425501, 0.02088673503, 
    0.02089925331, 0.02091181001, 0.02092440529, 0.02093703933, 
    0.02094971232, 0.02096242441, test1[18, 15]}, {0.10941526373, 
    0.017604005802, 0.017549654594, 0.017558686829, test1[19, 5], 
    test1[19, 6], test1[19, 7], test1[19, 8], test1[19, 9], 
    test1[19, 10], test1[19, 11], test1[19, 12], test1[19, 13], 
    test1[19, 14], test1[19, 15]}, {0.10890647143, 0.014986243752, 
    0.014927392616, 0.014934004368, test1[20, 5], test1[20, 6], 
    test1[20, 7], test1[20, 8], test1[20, 9], test1[20, 10], 
    test1[20, 11], test1[20, 12], test1[20, 13], test1[20, 14], 
    test1[20, 15]}};

Cases[Reverse@#, _Real, 1, 1] & /@ list // Flatten[#, 1] &

Result:

{0.0209624, 0.0175587, 0.014934}

$\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.