2
$\begingroup$

I have a program that outputs values for T in the following nested list:

{{{T->3398}, {T->56706}, {T->1.1049x10^7}, {T->9.192x10^7}}, {{T->3379}, {T->54664}, 
{T->1.07x10^7}, {T->8.769x10^7}}, {{T->3353}, {T->52293}, {T->1.04x10^7}, {T->8.346x10^7}}}

I want to use these values to make a table and plot them, but I can't work with the data as it is right now because it is in these nested lists and attached to T with the right arrow. I would like to be able to take the first value of T in each of the innermost lists, so 3398 3379 and 3353, but I am unable to do so. I've tried the flatten function and tried to create a solution with the Map function but mathematica only returns this same list. I should also note that my program runs a do loop 3 times to get those 3 innermost lists, and when I run the code after solving this problem I will run the do loop more times as I want to plot the first values as curves.

I should also note that in a separate file where I only run the do loop once I am able to flatten the list with a solution I found from another question. I thought I could apply this solution to the above problem and then take the first values but I was unable to do so. The problem is I need to run that do loop multiple times for my project so it doesn't help to be able to get the values for only running it once.

{{T->5416}, {T->1.532x10^6}, {T->3.85x10^6}, {T->1.399x10^8}}
list = %18
yay = {T} /. list
yayy = Flatten[yay]
ListPlot[yayy]

Can anybody help me to flatten the list so I can work with the values, specifically so I can grab the first T value from each innermost list? Any help is appreciated thank you!

$\endgroup$
2
  • $\begingroup$ Thank you for the answers this helps a lot $\endgroup$
    – captmonty
    Nov 9, 2022 at 16:34
  • $\begingroup$ don’t forget to accept one of the answers! It is helpful for yourself and others to do such a thing, especially when one of the answers may be better or more useful to you than the others. $\endgroup$ Nov 9, 2022 at 20:48

4 Answers 4

5
$\begingroup$

With your data called tlist, you can write:

Flatten[tlist /. {Rule -> List, T -> Nothing}]

and it gives the list of values:

{3398, 56706, 1.1049 x10^7, 9.192 x10^7, 3379, 54664, 1.07 x10^7, 
       8.769 x10^7, 3353, 52293, 1.04 x10^7, 8.346 x10^7}

This changes the Rule (i.e, the arrow ->) into a List and then changes all the Ts into nothing, leaving only the numerical values. Then you can Flatten.

$\endgroup$
4
$\begingroup$

Do you want this result?

list = {{{T -> 3398}, {T -> 56706}, {T -> 1.1049 x10^7}, {T -> 
      9.192 x10^7}}, {{T -> 3379}, {T -> 54664}, {T -> 
      1.07 x10^7}, {T -> 8.769 x10^7}}, {{T -> 3353}, {T -> 
      52293}, {T -> 1.04 x10^7}, {T -> 8.346 x10^7}}};
data = T /. list
data[[;; , 1]]

{3398, 3379, 3353}

ListPlot[data[[;; , 1]]]
$\endgroup$
2
$\begingroup$

One handy function when you're dealing with Rule expressions (that's what -> is) is Values:

orig = 
  {{{T -> 3398}, {T -> 56706}, {T -> 1.1049 x10^7}, {T -> 9.192 x10^7}}, 
   {{T -> 3379}, {T -> 54664}, {T -> 1.07 x10^7}, {T -> 8.769 x10^7}}, 
   {{T -> 3353}, {T -> 52293}, {T -> 1.04 x10^7}, {T -> 8.346 x10^7}}};
justVals = Values[orig]

{{{3398}, {56706}, {1.1049*x10^7}, {9.192*x10^7}}, {{3379}, {54664}, {1.07*x10^7}, {8.769*x10^7}}, {{3353}, {52293}, {1.04*x10^7}, {8.346*x10^7}}}

I would like to be able to take the first value of T in each of the innermost lists, so 3398 3379 and 3353

You can use part to do this:

justVals[[1, All, 1, 1]]

{3398, 3379, 3353}

Now, that looks a bit weird, but it's just because your original list had a lot of extraneous levels.

Can't help much with the rest of your question without seeing your Do loop.

$\endgroup$
2
$\begingroup$

Assuming your data is called tlist:

tlist = {{{T -> 3398}, {T -> 56706}, {T -> 1.1049 x10^7}, {T -> 
     9.192 x10^7}}, {{T -> 3379}, {T -> 54664}, {T -> 
     1.07 x10^7}, {T -> 8.769 x10^7}}, {{T -> 3353}, {T -> 
     52293}, {T -> 1.04 x10^7}, {T -> 8.346 x10^7}}}

It is often difficult to visually see structure in lists when written out in a flat form. Try one or more of the following:

TreeForm[tlist]

or better still in this case:

tlist // TableForm

$$\left( \begin{array}{cccc} \{T\to 3398\} & \{T\to 56706\} & \left\{T\to 1.1049 \text{x10}^7\right\} & \left\{T\to 9.192 \text{x10}^7\right\} \\ \{T\to 3379\} & \{T\to 54664\} & \left\{T\to 1.07 \text{x10}^7\right\} & \left\{T\to 8.769 \text{x10}^7\right\} \\ \{T\to 3353\} & \{T\to 52293\} & \left\{T\to 1.04 \text{x10}^7\right\} & \left\{T\to 8.346 \text{x10}^7\right\} \\ \end{array} \right)$$

This is a a fairly regular array structure.

To get the first column you can use the following commands.

First /@ tlist

or

tlist[[All, 1]]

and finally:

First /@ tlist // Values // Flatten

{3398, 3379, 3353}

$\endgroup$

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.