2012-01-14 37 views
0

我需要條件語句和行更新的幫助。如何使用條件if語句來更改行的元素是python

data = [("aa",1,.500,1), ("bb",3,000,1), ("cc",2,.750,1)] 


    foo = [] 
    bar = [] 
    Rdata = 15 
    for row in data: 
      if row[1] < Rdata: 
       foo.append(row) # creates foo table 

    #need help after this point    
    for row in foo: 
     if row[2] > 0: 
      bar.append(row) 

如何創建輸出表

bar = [("aa1.5",1,.500,1), ("bb3",3,000,1), ("cc2.75",2,.750,1)] 

編輯:數據集的更好的例子...

data2 = [(3,4,6,"aa","ab",1,.500,1,2,3), (1,2,1,"bb","ba",3,000,1,2,2), (4,5,5,"cc","cb",2,.750,1,2,3)] 

極品

data2 = [(3,4,6,"aa1.5","ab1.5",1,.500,1,2,3), (1,2,1,"bb3","ba3",3,000,1,2,2), (4,5,5,"cc2.75","cb2.75",2,.750,1,2,3)] 

回答

2
>>> [('%s%s' % (x[0], x[1] + x[2]),) + x[1:] for x in data] 
[('aa1.5', 1, 0.5, 1), ('bb3', 3, 0, 1), ('cc2.75', 2, 0.75, 1)] 
+0

作品AB但是whatif元素'aa'位於10的位置5中。 – Merlin 2012-01-14 03:14:06

+0

然後將'x [0]'更改爲'x [4]'。索引仍在索引。 – 2012-01-14 03:27:22

+0

我添加了數據集2以獲得更好的真實數據示例......我的想法是使用,但我更喜歡索引。 – Merlin 2012-01-14 03:34:39