Get value only from Position

If have the following command

Position[{a, b, a, a, b, c, b}, b]

(* Out[1]= {{2}, {5}, {7}} *)


Is it possible to get only a sequence of values instead of this list, like

Out[2]= 2, 5, 7


I'm interested in the values only and don't need the list structure.

-
Have a look at Flatten. – b.gatessucks Mar 24 '13 at 10:51
Out[2]=2,5,7 is not mathematica syntax. Do you mean Out[2]={2,5,7} ? – andre Mar 24 '13 at 11:17
Out[2]={2,5,7} is possible? how to do it @andre? If the output become 2, 5 , 7, how to manipulate it? so it come to be Out[2]=2,5,7? thanks – user6527 Mar 24 '13 at 11:42
Out[2]={2,5,7} is possible and trivial. To do it use Flatten, as suggested by @b.gatessucks. The question "How to manipulate it" as so many possible interpretations that I can't answer. Otherwise I don't understand : "so it come to be ..." – andre Mar 24 '13 at 11:58

The question is, what do you want to do with the output. The output of Position is in a form so that it can directly be used with Extract

list = {a, b, a, a, b, c, b};
pos = Position[list, b];
Extract[list, pos]
(* {b, b, b} *)


For this simple example, it is a bit useless because we already know, that on all positions pos we have a b in list. Unfortunately, your simple example covers another thing, which is that you really need the nested structure {{2}, {5}, {7}} in non-trivial cases.

If you really just want the values, than I suggest (as the others too) to use Flatten

Flatten[pos]
(* {2, 5, 7} *)


When your input is really just a one-dimensional structure or list, the above can be used with Part to extract the values of list

Part[list, Flatten[pos]]

(* or a bit shorter *)

list[[Flatten[pos]]]


In most cases you cannot simply flatten the output and to give you a small non-trivial example let's assume we have a somewhat moderate complex expression

 expr = 20 x^3 Cos[x + x^5] - Sin[x + x^5] - 10 x^4 Sin[x + x^5] - 25 x^8 Sin[x + x^5]


Now you want to use Position to find all positions where x appears with an exponent

pos = Position[expr, x^_]
(* {{1, 2}, {1, 3, 1, 2}, {2, 2, 1, 2}, {3, 2}, {3, 3, 1, 2}, {4, 2}, {4, 3, 1, 2}} *)


What you see is, that the structure is nested and this has a reason: To find the first x^_ which is at position {1,2} you have to select the first part which is

20 x^3 Cos[x + x^5]


and there the second one. Even more nested it gets, when the search pattern in located inside one of the Sin's. If you flatten this output of Position you get nothing useful but feeding the output to Extract gives you everything you wanted

Extract[expr, pos]

(* {x^3, x^5, x^5, x^4, x^5, x^8, x^5} *)

-
For the case of a vector, perhaps it is worth adding that his desired output can be used nicely with Part. +1 – Rojo Mar 24 '13 at 14:23
@Rojo Thanks and your suggestion is now included. – halirutan Mar 24 '13 at 15:00
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "http://mathematica.stackexchange.com/questions/21966/get-value-only-from-position/21974", "fetch_time": 1469653912000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2016-30/segments/1469257827079.61/warc/CC-MAIN-20160723071027-00058-ip-10-185-27-174.ec2.internal.warc.gz", "warc_record_offset": 160031853, "warc_record_length": 18481, "token_count": 899, "char_count": 2861, "score": 3.65625, "int_score": 4, "crawl": "CC-MAIN-2016-30", "snapshot_type": "latest", "language": "en", "language_score": 0.8658553957939148, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00018-of-00064.parquet"}