2016-10-19 319 views
0

我是新來的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) 

,我只想do是繪製x vs y,但這顯然不起作用,可能是出於多種原因,但至少因爲每個「值」都在方括號內(即列表本身)。我怎麼能阻止這些值(例如11,101,1001,10001)成爲列表?

從Fortran背景中,我與Python的列表,元組,數組numpy的陣列等,所有我希望做的是從一個文本文件,其內容(比如說)讀取大大掙扎11 0.0

101 0.0009751319885253906

1001 0.03459000587463379

10001 3.7970290184020996

1 00001 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]] 
>>> 

我希望這一切都有所幫助,我在此先感謝任何人提供任何幫助, o提供。

Simon。

+0

看起來您正在使用Numpy。我不認爲這種行爲是numpy獨有的,而只是一般的python語法。在我使用python的經驗中,我注意到[list.append()和list.extend()](http://stackoverflow.com/q/252703/1248974)之間的明顯區別。擴展列表往往有你看到的行爲,[文檔](https://docs.python.org/2/tutorial/datastructures.html#more-on-lists)將其解釋爲「通過擴展列表附加給定列表中的所有項目「,它可以像列表列表一樣,但是使用list.append()通常可以防止該問題。 – davedwards

+1

爲了告訴你如何防止這些值成爲列表,我們需要首先看看如何創建這些列表。如果是這樣,請[編輯](http://stackoverflow.com/posts/40120103/edit)你的問題,幷包括你如何在你的問題的某個地方創建這些列表。 – davedwards

+0

對於「課程」,澄清使用列表,元組,數組等,有許多免費的「教程」,以及完整的「課程」,有些是免費的,正如馬特麥卡洛在他的回答中提到的那樣。當然,還有付費課程,其中我發現非常有用的是[PluralSight](https://www.pluralsight.com/browse/software-development/python),特別是[Python基礎知識]( https://www.pluralsight.com/courses/python-fundamentals)課程。 – davedwards

回答

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