5
$\begingroup$

I have obtained an array of results as follows

{{{w -> 0.25}}, {{w -> 0.05}}, {{w -> 0.190065}, {w -> 
    0.0182686}}, {{w -> 0.0404842}, {w -> 0.0086731}}, {{w -> 
    0.156634}, {w -> 0.015429}, {w -> 0.00478921}}, {{w -> 
    0.0344873}, {w -> 0.00753932}, {w -> 0.00292085}}, {{w -> 
    0.134788}, {w -> 0.0134812}, {w -> 0.00424987}, {w -> 
    0.00191167}}, {{w -> 0.0302949}, {w -> 0.00671401}, {w -> 
    0.0026321}, {w -> 0.00131905}}, {{w -> 0.119177}, {w -> 
    0.012046}, {w -> 0.00383985}, {w -> 0.0017433}, {w -> 
    0.000948279}}}

I want to extract numbers only as an ordered list namely

{0.25, 0.05, 0.190065,0.0182686, ...}

when I use Flatten command I get first number of each inner list, How can extract all numbers as sorted list?

$\endgroup$
  • 3
    $\begingroup$ Values@list // Flatten or Flatten@list // Values or Values@Flatten@list or Flatten@Values@list $\endgroup$ – OkkesDulgerci Oct 14 at 15:11
9
$\begingroup$
a = {{{w -> 0.25}}, {{w -> 0.05}}, {{w -> 0.190065}, {w -> 
    0.0182686}}, {{w -> 0.0404842}, {w -> 0.0086731}}, {{w -> 
    0.156634}, {w -> 0.015429}, {w -> 0.00478921}}, {{w -> 
    0.0344873}, {w -> 0.00753932}, {w -> 0.00292085}}, {{w -> 
    0.134788}, {w -> 0.0134812}, {w -> 0.00424987}, {w -> 
    0.00191167}}, {{w -> 0.0302949}, {w -> 0.00671401}, {w -> 
    0.0026321}, {w -> 0.00131905}}, {{w -> 0.119177}, {w -> 
    0.012046}, {w -> 0.00383985}, {w -> 0.0017433}, {w -> 
    0.000948279}}}

w /. a // Flatten
$\endgroup$
8
$\begingroup$
Flatten[lst][[All, -1]]

{0.25, 0.05, 0.190065, 0.0182686, 0.0404842, 0.0086731, 0.156634, 0.015429, 0.00478921, 0.0344873, 0.00753932, 0.00292085, 0.134788, 0.0134812, 0.00424987, 0.00191167, 0.0302949, 0.00671401, 0.0026321, 0.00131905, 0.119177, 0.012046, 0.00383985, 0.0017433, 0.000948279}

Also

Values @ Flatten @ lst

same result

Flatten @ Values @ lst

same result

Cases[lst, _Real, All]

same result

$\endgroup$
6
$\begingroup$

For the general case where you only want rules with the left-hand-side 'w':

Cases[{{{w->0.25}},{{w->0.05}},{{w->0.190065},{w->0.0182686}},{{w->0.0404842},{w->0.0086731}},{{w->0.156634},{w->0.015429},{w->0.00478921}},{{w->0.0344873},{w->0.00753932},{w->0.00292085}},{{w->0.134788},{w->0.0134812},{w->0.00424987},{w->0.00191167}},{{w->0.0302949},{w->0.00671401},{w->0.0026321},{w->0.00131905}},{{w->0.119177},{w->0.012046},{w->0.00383985},{w->0.0017433},{w->0.000948279}}},
 (w->x_):>x,
 \[Infinity]
]
$\endgroup$
1
$\begingroup$

Another few ways centered around Level.

data = {{{w -> 0.25}}, {{w -> 0.05}}, {{w -> 0.190065}, {w -> 
 0.0182686}}, {{w -> 0.0404842}, {w -> 0.0086731}}, {{w -> 
 0.156634}, {w -> 0.015429}, {w -> 0.00478921}}, {{w -> 
 0.0344873}, {w -> 0.00753932}, {w -> 0.00292085}}, {{w -> 
 0.134788}, {w -> 0.0134812}, {w -> 0.00424987}, {w -> 
 0.00191167}}, {{w -> 0.0302949}, {w -> 0.00671401}, {w -> 
 0.0026321}, {w -> 0.00131905}}, {{w -> 0.119177}, {w -> 
 0.012046}, {w -> 0.00383985}, {w -> 0.0017433}, {w -> 
 0.000948279}}};

elems = Level[data, {-1}]
elems[[2 ;; ;; 2]]
Select[elems, NumericQ] == %
DeleteCases[elems, w] == %%

{w, 0.25, w, 0.05, w, 0.190065, w, 0.0182686, w, 0.0404842, w, \ 0.0086731, w, 0.156634, w, 0.015429, w, 0.00478921, w, 0.0344873, w, \ 0.00753932, w, 0.00292085, w, 0.134788, w, 0.0134812, w, 0.00424987, \ w, 0.00191167, w, 0.0302949, w, 0.00671401, w, 0.0026321, w, \ 0.00131905, w, 0.119177, w, 0.012046, w, 0.00383985, w, 0.0017433, w, \ 0.000948279}

{0.25, 0.05, 0.190065, 0.0182686, 0.0404842, 0.0086731, 0.156634, \ 0.015429, 0.00478921, 0.0344873, 0.00753932, 0.00292085, 0.134788, \ 0.0134812, 0.00424987, 0.00191167, 0.0302949, 0.00671401, 0.0026321, \ 0.00131905, 0.119177, 0.012046, 0.00383985, 0.0017433, 0.000948279}

True

True

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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