0
$\begingroup$

I have a file like this:

 primario =  980 pione n, en=   0.10229 GeV
 primario =  980 pione n, en=   0.10506 GeV
 primario =  980 pione p, en=   0.65135 GeV
 primario =  980 pione p, en=   0.44895 GeV
 primario =  980 pione p, en=   3.15166 GeV
 primario =  980 pione n, en=   1.03541 GeV
 primario =  989 pione n, en=   1.14979 GeV
 primario =  990 pione p, en=   0.72569 GeV
 primario =  992 pione p, en=   5.80965 GeV
 primario =  993 pione p, en=   0.24496 GeV
 primario =  993 pione n, en=   0.39453 GeV
 primario =  993 pione n, en=   1.07869 GeV
 primario =  997 pione p, en=   0.11673 GeV

I have to import only the number after en and separate the result into two lists of p and n. Using something like "FieldSeparators" -> {"=", ",", "primario", "en", "pione", "=", "GeV"} I should be able to import only the numbers and the strings p and n.

Now my problem is: I am left with a list like this

{{262, "p", 2.58277}, {272, "p", 1.17827}, {275, "p", 2.35279}, {281, "p", 0.17932}, {281, "p", 0.74644}, {292, "p", 1.44102}, {292, "p",0.41372}, {292, "p", 5.4164}, {297, "p", 0.38317}, {297, "p", 0.50133}, {297, "n", 0.91764}, {307, "p", 0.08304}, {307, "p", 0.16932}, {307, "n", 2.11982}, {307, "p", 2.78014}}

how do I separate the list into two lists containing only the last numbers? This lists, then, should be separated into a p list and a n list? The final lists should be something like this:

p list: {2.58277,1.17827,...} n list: {0.91764,...}

$\endgroup$
2
$\begingroup$

The easiest way would be to use GroupBy:

a = {{262, "p", 2.58277}, {272, "p", 1.17827}, {275, "p", 
2.35279}, {281, "p", 0.17932}, {281, "p", 0.74644}, {292, "p", 
1.44102}, {292, "p", 0.41372}, {292, "p", 5.4164}, {297, "p", 
0.38317}, {297, "p", 0.50133}, {297, "n", 0.91764}, {307, "p", 
0.08304}, {307, "p", 0.16932}, {307, "n", 2.11982}, {307, "p", 
2.78014}};
assoc = GroupBy[a, Extract[2] -> Last]

This groups the lists based on the second element of those lists, and the values it groups together is the last element of these lists. Then the list p and the list n can be found using:

assoc["p"]
assoc["n"]

Alternatively, it can be done using Select:

plist = Select[a,EqualTo["p"]@*Extract[2]][[All,3]]
nlist = Select[a,EqualTo["n"]@*Extract[2]][[All,3]]
$\endgroup$
1
$\begingroup$

Here are two simple options:

Using Cases

a = {{262, "p", 2.58277}, {272, "p", 1.17827}, {275, "p", 
    2.35279}, {281, "p", 0.17932}, {281, "p", 0.74644}, {292, "p", 
    1.44102}, {292, "p", 0.41372}, {292, "p", 5.4164}, {297, "p", 
    0.38317}, {297, "p", 0.50133}, {297, "n", 0.91764}, {307, "p", 
    0.08304}, {307, "p", 0.16932}, {307, "n", 2.11982}, {307, "p", 
    2.78014}};

Cases[a, {_, "p", i_} :> i]

{2.58277, 1.17827, 2.35279, 0.17932, 0.74644, 1.44102, 0.41372, 5.4164, 0.38317, 0.50133, 0.08304, 0.16932, 2.78014}

Cases[a, {_, "n", i_} :> i]

{0.91764, 2.11982}

Or using Select

Select[a, #[[2]] == "p" &][[All, 3]]
Select[a, #[[2]] == "n" &][[All, 3]]
$\endgroup$
0
$\begingroup$

One way is to use Sow and Reap. For example:

a = {{262, "p", 2.58277}, {272, "p", 1.17827}, {275, "p", 
    2.35279}, {281, "p", 0.17932}, {281, "p", 0.74644}, {292, "p", 
    1.44102}, {292, "p", 0.41372}, {292, "p", 5.4164}, {297, "p", 
    0.38317}, {297, "p", 0.50133}, {297, "n", 0.91764}, {307, "p", 
    0.08304}, {307, "p", 0.16932}, {307, "n", 2.11982}, {307, "p", 
    2.78014}};

res = Reap[Sow[#1[[3]], #1[[2]]] & /@ a, _, Rule][[2]]

/* {"p" -> {2.58277, 1.17827, 2.35279, 0.17932, 0.74644, 1.44102, 
   0.41372, 5.4164, 0.38317, 0.50133, 0.08304, 0.16932, 2.78014}, 
 "n" -> {0.91764, 2.11982}} */

And then use ReplaceAll to give the individual lists, e.g.

"n" /. res

/* {0.91764, 2.11982} */
$\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.