取下方括号? (新手)

时间:2016-10-19 00:22:27

标签: python arrays plot data-fitting

我是Python的新手,我最终得到了两个列表列表,每个列表都包含一个整数(在下面的y中浮动),如下所示:

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

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

我想做的就是绘制x vs y,但这显然不会起作用,可能有很多原因,但至少因为每个“值”都在方括号中(即列表本身)。我怎么能阻止这些值(例如11,101,1001,10001)成为列表?

从Fortran背景来看,我在Python的列表,元组,数组,numpy数组等方面都在苦苦挣扎。我想做的是从一个文本文件中读取内容(例如):

11 0.0

101 0.0009751319885253906

1001 0.03459000587463379

10001 3.7970290184020996

100001 498.934268951416

并将第一个'列'读为x,将第二个'列'读为y,目的是绘制此数据。

任何人都可以推荐一个在线课程,澄清列表,元组,数组等的用途吗?

非常感谢提前。

编辑:为了回应人们的意见和建议,我在运行结束时包括我使用的代码,输入文件内容和交互式窗口输出。

非常感谢所有回应我的人;我发现所有意见和建议都非常有帮助。我会对所有这些反应采取行动并尝试为自己解决问题,但如果有人能看一下我的代码,'输入文件'内容和交互式窗口'输出',看看他们是否可以帮助我,我将不胜感激进一步。我再次非常感谢人们为此与我沟通的时间和精力。

以下是代码:

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)

以下是代码的文件内容:

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

最后,这是执行后出现在交互式窗口中的内容:

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

我希望这一切都有所帮助,我提前感谢任何人提供的任何帮助。

西蒙。

1 个答案:

答案 0 :(得分:1)

如果没有进入从文件中读取的代码,您首先要使用元组列表设置程序。

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

如果你有一个想要拉出坐标的文件,可以试试这样的代码:

#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

希望此解决方案可以帮助您处理列表。如果您需要有关基本python的更多信息,请查看https://www.codecademy.com/learn/python