Remove the square brackets? (Newbie)
<p>I am new to Python and I have somehow ended up with two lists of lists, each of which contains a single integer (float in y below), as follows:</p>

<pre><code>&gt;x
array([[11], [101], [1001], [10001], [100001]], dtype=object)

&gt;y
array([[0.0], [0.0009751319885253906], [0.03459000587463379],
   [3.7970290184020996], [498.934268951416]], dtype=object)
</code></pre>

<p>All I wish to do is to plot x vs y, but this clearly won't work, probably for a number of reasons, but at least because each 'value' is in square brackets (i.e. a list in itself).  How could I have prevented these values (e.g. 11, 101, 1001, 10001) from becoming lists?</p>

<p>Being from a Fortran background, I am struggling greatly with Python's lists, tuples, arrays, numpy arrays, etc.  All I wish to do is to read from a text file whose contents are (say):</p>

<p>11  0.0</p>

<p>101  0.0009751319885253906</p>

<p>1001  0.03459000587463379</p>

<p>10001  3.7970290184020996</p>

<p>100001  498.934268951416</p>

<p>and to read the first 'column' as x and the second 'column' as y, with the aim of plotting this data.</p>

<p>Can anyone please recommend an online course which clarifies the use of lists, tuples, arrays, etc for this kind of thing?</p>

<p>Many thanks in advance.</p>

<p>EDIT:  In response to people's comments and suggestions, I am including my code used, the input file content and the interactive window output at the end of the run.</p>

<p>Many thanks to everyone who has responded to me; I have found all comments and suggestions to be very helpful.  I shall act on all of these responses and try to figure things out for myself, but I would appreciate it if anyone could take a look at my code, 'input file' contents and interactive window 'output' to see if they can help me further.  Again, I really appreciate the time and effort people have put in to communicate with me about this.</p>

<p>Here is the code:</p>

<pre><code>import re
import numpy as np
import time
import pandas as pd


def dict2mat(res1, res2):
#
#  input 2 dictionaries and return the content of the first as x
#  and the content of the second as y
#
    s = pd.Series(res1)
    x = s.values
    s = pd.Series(res2)
    y = s.values

    return x, y


f = open('results.txt', 'r')
nnp = {}
tgen = {}
tconn = {}
tcalc = {}
tfill = {}
iline = 0
for i in range(1000):
    line = f.readline()
    if "Example" in line:
#
# first line of text having numerical values of interest contains 
# the string 'Example'
#
        iline = iline+1
#
# extract number of nodes (integer)
#
        nnp[iline] = [int(s) for s in re.findall(r"\d+", line)]
        line = f.readline()
#
# extract time taken to generate data set (float)
#
        tgen[iline] = [float(s) for s in re.findall(r"\d+[\.]\d+", line)]
        line = f.readline()
#
# extract time taken to generate connectivity data (float)
#
        tconn[iline] = [float(s) for s in re.findall(r"\d+[\.]\d+", line)]
        line = f.readline()
#
# extract time taken to calculate error (float) for corners
#
        tcalc[iline] = [float(s) for s in re.findall(r"\d+[\.]\d+", line)]
        line = f.readline()
#
# extract time taken to fill in stress results at midsides (float)
#
        tfill[iline] = [float(s) for s in re.findall(r"\d+[\.]\d+", line)]

#
# use function dict2mat to replace the contents of 'number of nodes' 
# and each of the 'times' in turn by vectors x and y
#
xgen, ygen = dict2mat(nnp, tgen)
xconn, yconn = dict2mat(nnp, tconn)
xcalc, ycalc = dict2mat(nnp, tcalc)
xfill, yfill = dict2mat(nnp, tfill)

# get x and y vectors
x = np.array(xgen)
y = np.array(ygen)
print('x: ')
print(x)
print('y: ')
print(y)
</code></pre>

<p>Here is the content of the file which the code reads from:</p>

<pre><code>Random seed used to form data = 9001
Example has 11 generated global surface nodes
Time taken to generate the data: --- 0.002001047134399414 seconds ---
Time taken to find connectivity: --- 0.0 seconds ---
Time taken to calculate Stress Error for corner nodes only: --- 0.0004999637603759766 seconds ---
Time taken to fill-in midside node Stress Errors: --- 0.0 seconds ---




Random seed used to form data = 9001
Example has 101 generated global surface nodes
Time taken to generate the data: --- 0.01451420783996582 seconds ---
Time taken to find connectivity: --- 0.0 seconds ---
Time taken to calculate Stress Error for corner nodes only: --- 0.004984855651855469 seconds ---
Time taken to fill-in midside node Stress Errors: --- 0.0009751319885253906 seconds ---




Random seed used to form data = 9001
Example has 1001 generated global surface nodes
Time taken to generate the data: --- 0.10301804542541504 seconds ---
Time taken to find connectivity: --- 0.0 seconds ---
Time taken to calculate Stress Error for corner nodes only: --- 0.04008197784423828 seconds ---
Time taken to fill-in midside node Stress Errors: --- 0.03459000587463379 seconds ---




Random seed used to form data = 9001
Example has 10001 generated global surface nodes
Time taken to generate the data: --- 1.0397570133209229 seconds ---
Time taken to find connectivity: --- 0.0 seconds ---
Time taken to calculate Stress Error for corner nodes only: --- 0.41377687454223633 seconds ---
Time taken to fill-in midside node Stress Errors: --- 3.7970290184020996 seconds ---




Random seed used to form data = 9001
Example has 100001 generated global surface nodes
Time taken to generate the data: --- 10.153867959976196 seconds ---
Time taken to find connectivity: --- 0.0 seconds ---
Time taken to calculate Stress Error for corner nodes only: --- 3.938124895095825 seconds ---
Time taken to fill-in midside node Stress Errors: --- 498.934268951416 seconds ---
</code></pre>

<p>Finally, this is what appears in the interactive window after execution:</p>

<pre><code>x: 
&gt;&gt;&gt; print(x)
[[11] [101] [1001] [10001] [100001]]
&gt;&gt;&gt; print('y: ')
y: 
&gt;&gt;&gt; print(y)
[[0.002001047134399414] [0.01451420783996582] [0.10301804542541504]
 [1.0397570133209229] [10.153867959976196]]
&gt;&gt;&gt; 
</code></pre>

<p>I hope this all helps and I thank anyone in advance for any help they are able to provide.</p>

<p>Simon.</p>

<p>Without getting into the code behind reading from a file, you'll first want to setup your program with a list of tuples.</p>

<pre><code>#Example empty list
points = []

#x,y = (1, 2) assigns 1 to x and 2 to y
x,y = (1, 2)

#this appends the tuple (x, y) into the points list
points.append((x, y))
</code></pre>

<p>If you have a file that you want to pull the coordinates in from try some code like this:</p>

<pre><code>#Example empty list
points = []

filename = "myfile.txt"
file_with_points = open(filename, "r")

for line in file_with_points.readlines():
    #assume the points are separated by a space
    splitline = line.split(" ")
    x, y = splitline[0], splitline[1]
    points.append((x, y))

file_with_points.close()
print points
</code></pre>

<p>Hopefully this solution helped you work with lists. If you need more info on really basic python check out <a href="https://www.codecademy.com/learn/python" rel="nofollow">https://www.codecademy.com/learn/python</a></p>

python arrays plot data-fitting