我有一段代码,这是一个在字典循环中调用的函数,它如下:
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.....]
这样排序和排序的函数就可以使用它。
答案 0 :(得分:0)
只需将for语句从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