0

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:

>x
array([[11], [101], [1001], [10001], [100001]], dtype=object)

>y
array([[0.0], [0.0009751319885253906], [0.03459000587463379],
   [3.7970290184020996], [498.934268951416]], dtype=object)

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?

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):

11 0.0

101 0.0009751319885253906

1001 0.03459000587463379

10001 3.7970290184020996

100001 498.934268951416

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

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

Many thanks in advance.

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.

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.

Here is the 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)

Here is the content of the file which the code reads from:

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 ---

Finally, this is what appears in the interactive window after execution:

x: 
>>> print(x)
[[11] [101] [1001] [10001] [100001]]
>>> print('y: ')
y: 
>>> print(y)
[[0.002001047134399414] [0.01451420783996582] [0.10301804542541504]
 [1.0397570133209229] [10.153867959976196]]
>>> 

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

Simon.

  • Looks like you're working with Numpy. I don't think this behavior is unique to numpy, but rather it's just general python syntax. In my experience with python, I noticed the distinct difference between [list.append() and list.extend()](http://stackoverflow.com/q/252703/1248974) . Extending a list tends to have the behavior you're seeing, [the docs](https://docs.python.org/2/tutorial/datastructures.html#more-on-lists) explain it as "Extend the list by appending all the items in the given list", which can be like a list of lists, yet using `list.append()` generally prevents that problem. – chickity china chinese chicken Oct 19 '16 at 00:44
  • 1
    To advise you on how to prevent those values from becoming lists, we'd need to see how to created those lists in the first place. If so, please [edit](http://stackoverflow.com/posts/40120103/edit) your question and include how you created those lists somewhere in your question. – chickity china chinese chicken Oct 19 '16 at 00:58
  • For "courses" that clarify the use of lists, tuples, arrays, etc, there are many free "tutorials" as well as full "courses", some free, as Matt McCullough mentioned one of such in his answer. There are also, of course, paid courses, one of which I've found highly helpful is [PluralSight](https://www.pluralsight.com/browse/software-development/python), particularly the [Python Fundamentals](https://www.pluralsight.com/courses/python-fundamentals) course. – chickity china chinese chicken Oct 19 '16 at 01:04
  • I have now edited my original question to demonstrate how I arrived at the list of lists in the first place. – Simon Hannaby Oct 19 '16 at 16:34
  • Thanks for updating the information. It looks like the culprit that is causing those values to become lists inside of lists is the extraction of integers lines performing `regex` search. `re.findall()` returns a list of all items found that match the pattern, in this case it still creates a list of one item. To avoid that, we can add a `[0]` on the end of the assignment line, which returns only the first item (see this explained here: http://stackoverflow.com/a/25050328/1248974. Change each `re.findall()` to look like `re.findall(r"\d+", line)[0]` – chickity china chinese chicken Oct 19 '16 at 19:29

1 Answers1

1

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

#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))

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

#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

Hopefully this solution helped you work with lists. If you need more info on really basic python check out https://www.codecademy.com/learn/python