2016-02-19 44 views
-1

所以我有一個函數在這裏假設返回一個包含程序運行時會發生什麼的字典。我無法發佈完整的程序,因爲我可能會遇到麻煩,但是我編寫了我知道適用於我的程序的代碼,並且我想知道它是否會以更簡單的方式佔用更少的線或更長的路。Python重寫代碼,返回包含統計信息的字典

def start(self, startingevents): 
    """Run the simulation on list of events in <startingevents>. 

    Return a dict containing statistics of the simulation. 
    @type self: Simulation 
    @type initial_events: list[Event] 
     An initial list of events. 
    @rtype: dict[str, object] 
    """ 
    eventcount = [] 

    for event in startingevents: 
     eventcount.add(event) 

    for event in eventcount: 
     new_events = event.do(self.dispatcher, self.monitor) 
     for new_event in new_events: 
      eventcount.add(new_event) 
    return self._monitor.report() 
+0

返回一個列表,不是字典。 –

+0

@JohnGordon固定它的最後一行雖然是程序的一部分,它會返回一個字典。 – ZeroKEz

+1

您在eventcount中的事件:... eventcount.add(new_event)行可以被簡化爲event.ount(self.dispatcher,self.monitor)中的[eventcount.add(new_event)事件eventcount中的new_event。 ]' – zondo

回答

0

我不知道,它是否會做的事,但它可能是值得給它一個嘗試:

eventcount = [ new_element 
       for event in startingevents 
       for new_element in event.do(self.dispatcher, self.monitor) 
      ] 

所有for循環,而不是