2017-02-07 148 views
0

我與測量值的列表,如:「列表」對象沒有屬性「方法」

[[Measurements(100, 0.3)], 
[Measurements(33, 0.5)]] 

然後,我有一些標準的列表,其包含的測量列表:

[Criteria(999, [[Measurements(100, 0.3)], 
         [Measurements(33, 0.5)]])], 

[Criteria(999, [[Measurements(150, 0.3)], 
         [Measurements(35, 0.5)]])] 

最後,我想提供上述列表作爲輸入並執行代碼:

class Measurements(): 
    def __init__(self, value, other): 
     self.value = value 
     self.other = other 


class Criteria(): 
    def __init__(self, new_value, measurements): 
     self.new_value = new_value 
     self.measurements = measurements 

    def method(self): 

     # Here, I am iterating through all measurements 
     # and replace the value according to criteria 
     for idx, x in enumerate(self.measurements): 
      if (self.measurements[idx].value > 20 and 
        self.measurements[idx].value < 110): 

       self.measurements[idx].value = self.new_value 
     return self.measurements 


class Evaluate(): 
    def __init__(self, criteria): 
     self.criteria = criteria 

    def execute(self): 
     criteria = self.criteria 

     # Here, I am iterating through all criteria 
     # and call the method from criteria 
     for i, c in enumerate(criteria): 
      c.method() 
      return c.measurements 


def main(): 

    criteria = [ 
     [Criteria(999, [[Measurements(100, 0.3)], 
         [Measurements(33, 0.5)]])], 

     [Criteria(999, [[Measurements(150, 0.3)], 
         [Measurements(35, 0.5)]])] 
    ] 

    obs = (Evaluate(criteria).execute()) 


if __name__ == "__main__": 
    main() 

和我收到c.method() AttributeError: 'list' object has no attribute 'method'

我想我的結果是新的校正測量,所以:

[[Measurements(999, 0.3)], 
[Measurements(999, 0.5)]], 
[Measurements(150, 0.3)], 
[Measurements(999, 0.5)]] 
+4

爲什麼你有嵌套的列表? 'C'是'[標準(999,...)]',在它的一個元件的列表,*不*'標準(999,...)'。 –

+0

我有嵌套列表,因爲我會有很多標準,我想評估。 – George

+1

那並不需要*嵌套*名單.. –

回答

2

你所有的Criteria()Measurement()對象中的每個存儲在一個元素的列表,而這又是存儲在一個較大名單。要麼爲此考慮,要麼不使用單元素嵌套列表。

我沒有看到這些單元素列表的任何理由;下面的定義應該讓你的代碼工作:

criteria = [ 
    Criteria(999, [Measurements(100, 0.3), Measurements(33, 0.5)]), 
    Criteria(999, [Measurements(150, 0.3), Measurements(35, 0.5)]), 
] 

你反之則要要麼提取每一次,或使用嵌套循環一個元素:

def execute(self): 
    for c in self.criteria: 
     c[0].method() 

def execute(self): 
    for c_list in self.criteria: 
     for c in c_list: 
      c.method() 

您還應該而不是for循環中使用return,至少在您想要測量全部標準。將測量值收集到列表中,然後返回該列表。

您還在Criteria.method()循環中使用self太多次。不需要enumerate(),您不需要索引來更改可變對象。

更正代碼:

class Measurements(): 
    def __init__(self, value, other): 
     self.value = value 
     self.other = other 


class Criteria(): 
    def __init__(self, new_value, measurements): 
     self.new_value = new_value 
     self.measurements = measurements 

    def method(self): 
     for measurement in self.measurements: 
      if 20 < measurement.value < 110: 
       measurement.value = self.new_value 
     return self.measurements 

class Evaluate(): 
    def __init__(self, criteria): 
     self.criteria = criteria 

    def execute(self): 
     results = [] 
     for c in self.criteria: 
      measurements = c.method() 
      results.extend(measurements) 
     return results 

def main(): 
    criteria = [ 
     Criteria(999, [Measurements(100, 0.3), Measurements(33, 0.5)]), 
     Criteria(999, [Measurements(150, 0.3), Measurements(35, 0.5)]), 
    ] 

    obs = Evaluate(criteria).execute() 
+0

好了,感謝您的幫助和建議!所以,我的主要問題是嵌套列表,並沒有在所有的循環工作!至於回報,在它會只返回第一個計算,對嗎?(upvoted) – George

+0

@George:完全; 'return'將在那裏退出函數,所以只返回第一個結果。 –

+0

謝謝you.Can你告訴我[此情況下(http://pastebin.com/Dc7dzaVp)我怎麼能對付回報,如果我有很多情況下? (我的意思是,好,我可以有很多的在任何情況下的循環,但我不知道是否還有別的東西) – George