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.....]

这样 sort 和 sorted 函数将对其起作用。

4

1 回答 1

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
于 2012-07-18T17:21:39.247 回答