[英]Remove the square brackets? (Newbie)
我是Python的新手,我以某种方式结束了两个列表列表,每个列表包含一个整数(以下y中的float),如下所示:
>x
array([[11], [101], [1001], [10001], [100001]], dtype=object)
>y
array([[0.0], [0.0009751319885253906], [0.03459000587463379],
[3.7970290184020996], [498.934268951416]], dtype=object)
我要做的只是绘制x与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]]
>>>
希望对您有所帮助,并在此先感谢任何人能够提供的任何帮助。
西蒙
在不涉及读取文件的背后代码的情况下,您首先需要使用元组列表来设置程序。
#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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.