2014-02-22 23 views
0

我有一個包含子列表有3個值列表生成一個表,我需要打印出來,看起來像一個列表:如何從一個列表

list picture

我還需要比較第三列值與海誓山盟,告訴他們是否正在增加或減少,因爲你下去。

bb = 3.9 
lowest = 0.4 
#appending all the information to a list 
allinfo= [] 
while bb>=lowest: 
    everything = angleWithPost(bb,cc,dd,ee) 
    allinfo.append(everything) 
    bb-=0.1 

我認爲找出是否是第三列值的總體思路是增加或減少是:

#Checking whether or not Fnet are increasing or decreasing 
ii=0 
while ii<=(10*(bb-lowest)): 
    if allinfo[ii][2]>allinfo[ii+1][2]: 
     abc = "decreasing" 
    elif allinfo[ii][2]<allinfo[ii+1][2]: 
     abc = "increasing" 
    ii+=1 

然後,當我想打印出類似於上面的一個我的表。

jj=0 
while jj<=(10*(bb-lowest)) 
    print "%8.2f %12.2f %12.2f %s" %(allinfo[jj][0], allinfo[jj][1], allinfo[jj][2], abc) 
    jj+=1 

這裏是部分

def chainPoints(aa,DIS,SEG,H): 
    #xtuple x chain points 
    n=0 
    xterms = [] 
    xterm = -DIS 
    while n<=SEG: 
     xterms.append(xterm) 
     n+=1 
     xterm = -DIS + n*2*DIS/(SEG) 
# 
    #ytuple y chain points 
    k=0 
    yterms = [] 
    while k<=SEG: 
     yterm = H + aa*m.cosh(xterms[k]/aa) - aa*m.cosh(DIS/aa) 
     yterms.append(yterm) 
     k+=1 
    return(xterms,yterms) 
# 
# 
def chainLength(aa,DIS,SEG,H): 
    xterms, yterms = chainPoints(aa,DIS,SEG,H)# using x points and y points from the chainpoints function 
    #length of chain 
    ff=1 
    Lterm=0. 
    totallength=0. 
    while ff<=SEG: 
     Lterm = m.sqrt((xterms[ff]-xterms[ff-1])**2 + (yterms[ff]-yterms[ff-1])**2) 
     totallength += Lterm 
     ff+=1 
    return(totallength) 
# 

def angleWithPost(aa,DIS,SEG,H): 
    xterms, yterms = chainPoints(aa,DIS,SEG,H) 
    totallength = chainLength(aa,DIS,SEG,H) 
    #Find the angle 
    thetaradians = (m.pi)/2. + m.atan(((yterms[1]-yterms[0])/(xterms[1]-xterms[0]))) 
    #Need to print out the degrees 
    thetadegrees = (180/m.pi)*thetaradians 
    #finding the net force 
    Fnet = abs((rho*grav*totallength))/(2.*m.cos(thetaradians)) 
    return(totallength, thetadegrees, Fnet) 
+1

爲什麼不直接使用模塊列表?它非常適合二維陣列。 https://pypi.python.org/pypi/tabulate –

+0

我不能使用我需要安裝的東西。這一切都必須是預先包裝到我的課程使用的程序中的東西。那個程序是Canopy –

+1

好吧...它有點醜,但它需要一個二維數組作爲輸入。看看http://code.activestate.com/recipes/578801-pretty-print-table-in-tabular-format/ –

回答

1

基本上你想第4列添加到內部列表和打印結果的角度?

#print headers of table here, use .format for consistent padding 

previous = 0 
for l in outer_list: 
    if l[2] > previous: 
     l.append('increasing') 
    elif l[2] < previous: 
     l.append('decreasing') 
    previous = l[2] 

    #print row here use .format for consistent padding 

更新的元組的列表,add value to tuple

import random 
outer_list = [ (i, i, random.randint(0,10),)for i in range(0,10)] 

previous = 0 
allinfo = [] 
for l in outer_list: 
    if l[2] > previous: 
     allinfo.append(l +('increasing',)) 
    elif l[2] < previous: 
     allinfo.append(l +('decreasing',)) 
    previous = l[2] 

    #print row here use .format for consistent padding 

print(allinfo) 

這最肯定可以優化,你可以減少你迭代數據的次數。

+0

我認爲這可能會解決它,但由於某種原因,我的代碼說我不能追加一個元組,但我知道我有我的「allinfo []」作爲列表。 –

+0

我應該說,這是指與l.append('遞減') –

+0

行我沒有捕捉到'一切'是一個元組。我會更新。 –

1

查看這個使用map和迭代器技巧的Python2實現。

from itertools import izip_longest, islice 
from pprint import pprint 


data = [ 
    [1, 2, 3], 
    [1, 2, 4], 
    [1, 2, 3], 
    [1, 2, 5], 
] 


class AddDirection(object): 
    def __init__(self): 
     # This default is used if the series begins with equal values or has a 
     # single element. 
     self.increasing = True 

    def __call__(self, pair): 
     crow, nrow = pair 
     if nrow is None or crow[-1] == nrow[-1]: 
      # This is the last row or the direction didn't change. Just return 
      # the direction we previouly had. 
      inc = self.increasing 
     elif crow[-1] > nrow[-1]: 
      inc = False 
     else: 
      # Here crow[-1] < nrow[-1]. 
      inc = True 
     self.increasing = inc 

     return crow + ["Increasing" if inc else "Decreasing"] 

result = map(AddDirection(), izip_longest(data, islice(data, 1, None))) 

pprint(result) 

輸出:

pts/1$ python2 a.py 
[[1, 2, 3, 'Increasing'], 
[1, 2, 4, 'Decreasing'], 
[1, 2, 3, 'Increasing'], 
[1, 2, 5, 'Increasing']] 

每當你想改變一個列表的內容(在這種情況下的行列表),地圖是一個很好的地方,開始思考。

當算法需要列表中多個位置的數據時,偏移列表並壓縮所需的值也是一項強大的技術。使用生成器以使列表不必被複制,使得這在真實代碼中可行。

最後,當需要保持呼叫之間的狀態(在這種情況下是方向)時,使用對象是最佳選擇。

對不起,如果代碼太簡潔!

+0

謝謝你的幫助,但不幸的是,這對我正在上課的班級來說太過先進。我必須將我的課程保持在所教授的領域之內。 –

相關問題