2016-10-19 2 views
0

내가 파이썬에 새로운 오전 다음과 같이 어떻게 든 하나의 정수 (아래 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를 플롯하는 것입니다.하지만 이것은 여러 가지 이유 때문에 분명히 작동하지 않을 것입니다.하지만 적어도 각 'value'는 대괄호 (즉, 목록 자체)에 있기 때문에 작동하지 않습니다. 이 값 (예 : 11, 101, 1001, 10001)이 목록이되지 않도록하려면 어떻게해야합니까?

: 포트란 배경에서 인

, I 등 그 내용 (예를 들어)있는 텍스트 파일에서 읽을 수 있습니다 내가 원하는 모든 파이썬의리스트, 튜플, 배열, NumPy와 배열에 따라 크게 어려움을 겪고 있어요 0.0009751319885253906

1,001 0.03459000587463379

10001 3.7970290184020996

1 11 0.0

101 00001 498.934268951416

그리고 첫 번째 '열'을 x로, 두 번째 '열'을 y로 읽으므로이 데이터를 플롯해야합니다.

누구나 이런 종류의 목록, 튜플, 배열 등의 사용을 명확히하는 온라인 과정을 추천 할 수 있습니까?

미리 감사드립니다.

EDIT : 사람들의 의견과 제안에 따라 사용 된 코드, 입력 파일 내용 및 실행 종료시 대화 형 창 출력을 포함합니다.

저에게 반응 한 모든 사람들에게 많은 감사를드립니다. 나는 모든 의견과 제안이 매우 도움이된다는 것을 발견했다. 나는이 모든 반응에 따라 행동하고 스스로 해결하려고 노력할 것이다. 그러나 누군가 내 코드, '입력 파일'의 내용 및 대화식 창 '출력'을보고 그들이 나를 도울 수 있는지 알아봐 주시면 고맙겠습니다. 더욱이. 다시 한 번, 사람들이 이것에 관해 나와 대화하기 위해 투입 한 시간과 노력에 정말로 감사드립니다.

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

마지막으로,이 실행 후 대화 창에 나타납니다 것입니다 : 여기

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) 

이 코드에서 읽는 파일의 내용입니다 : 여기

코드입니다

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

이 모두가 도움이되기를 바랍니다. 사전에 도움을받을 수있는 사람에게 감사드립니다. 제공합니다.

사이먼.

+0

Numpy와 같이 작업하는 것 같습니다. 나는이 행동이 numpy에만 국한된 것이 아니라, 일반적인 python 구문이라고 생각한다. 파이썬에 대한 나의 경험에서, 나는 [list.append()와 list.extend()] (http://stackoverflow.com/q/252703/1248974)의 뚜렷한 차이점을 발견했다. 목록을 확장하는 것은 당신이 보는 행동을하는 경향이 있습니다. [docs] (https://docs.python.org/2/tutorial/datastructures.html#more-on-lists)는 "목록을 다음과 같이 확장합니다. 리스트의리스트와 같을 수 있지만'list.append()'를 사용하는 것은 일반적으로 그 문제를 방지합니다. – davedwards

+1

이러한 값이 목록이되지 않도록하는 방법에 대한 조언을 얻으려면 먼저 해당 목록을 만드는 방법을 알아야합니다. 그렇다면 질문을 편집하고 (http://stackoverflow.com/posts/40120103/edit) 해당 목록을 어떻게 작성했는지 포함하십시오. – davedwards

+0

목록, 튜플, 배열 등의 사용을 명확하게하는 "과정"의 경우 매트 맥 컬러 (Matt McCullough)가 자신의 대답에서 언급 한 것처럼 무료 "튜토리얼"과 풀 "코스"가 무료입니다. 물론 유료 코스 중 하나는 [PluralSight] (https://www.pluralsight.com/browse/software-development/python), 특히 [Python Fundamentals] (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 

는 희망이 솔루션은 목록 작업을 도왔다. 정말 기본적인 파이썬 체크 아웃에 대한 자세한 정보가 필요하면 https://www.codecademy.com/learn/python