简体   繁体   中英

Remove the square brackets? (Newbie)

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 (ie a list in itself). How could I have prevented these values (eg 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM