2012-07-18 137 views
0

我有一段代码,这是一种被称为一个循环在字典中的功能列表,它如下:生产可以排序

hope = [] 
seconds = [] 
hope.append(self.date) 
for those in hope: 
    date = those 
    pattern = '%m/%d/%Y' 
    epoch = int(time.mktime(time.strptime(date, pattern))) 
    seconds.append(epoch) 
    print seconds 

我得到的结果一样

[1505084400] 
[1500850800] 
[1509926400] 
[1496617200] 
[1492383600] 
[1488758400] 
[1499036400] 
[1511136000] 
[1511136000] 
… 

但我想几秒钟的结果是这样的:

[1505084400,1500850800,1509926400,1496617200,1492383600,1488758400,1499036400,1511136000,1511136000.....] 

这样的排序和分类功能将进行这项工作。

+2

你'for'循环始终只有一个迭代,因为'hope'是一个长的名单。看起来对我来说毫无意义,但我不知道这段代码应该做些什么。 – 2012-07-18 17:16:36

+1

对于我来说听起来像'seconds'需要成为一个成员变量或参数,因为每次调用方法时,都会创建一个新的'seconds'列表。 – GWW 2012-07-18 17:17:44

+0

下次请发布完整的课程定义。如果没有init,我们不得不猜测你的代码在做什么的某些部分。我当然假设你使用一个类,因为在你的代码中有一个对自己的引用。 – 2012-07-18 17:26:00

回答

0

就拿print语句出来的for循环和保持名单的状态:

class someclass(object): 
    def __init__(self): 
     self.hope = [] 
     self.seconds = [] 
    def appendtoseconds(self): 
     self.hope.append(self.date) 
     for those in self.hope: 
      date = those 
      pattern = '%m/%d/%Y' 
      epoch = int(time.mktime(time.strptime(date, pattern))) 
      self.seconds.append(epoch) 
     print self.seconds 
+0

'self.seconds'将以这种方法进行二次增长,因为每次调用appendtoseconds()时都会重申'self.hope'。 – 2012-07-18 17:27:00

+0

的确如此,但由于除了构建可排序的持久列表之外,我们还不知道他正在尝试完成什么,所以它仍能合理地回答所提问题 – 2012-07-18 17:29:08