在项目列表中重复一次?

时间:2017-11-08 11:08:40

标签: python python-3.x

我现在一直在努力解决这个黑客挑战,我似乎无法弄明白。

我已经能够找到一个我对一堆信息进行排序的部分,但是我很难理解如何结合一些事情

https://i.imgur.com/IVTA3TU.png

如您所见,我在“您的输出”框中有一个副本,日期为2015-12。我怎样才能将它们梳理成“预期输出”?

谢谢!

这是我到目前为止编写的代码

from datetime import date
from collections import Counter

line = input()

startDate = line.split(',')[0]
startYear, startMonth = startDate.split('-')
startDate = date(int(startYear), int(startMonth), 1)

endDate = line.split(' ')[1]
endYear, endMonth = endDate.split('-')
endDate = date(int(endYear), int(endMonth), 1)

line = input()

d =[]
while True:
    try:
        line = input()
        currentDate = date(int(line.split('-')[0]), int(line.split('-')[1].split('-')[0]), 1)

        if startDate <= currentDate < endDate:
            engagement = line.split(',')[1].strip()
            totalEngage = line.split(',')[2].strip()
            correctDate = "{}-{}".format(currentDate.year, str(currentDate.month).zfill(2))
            d.append([correctDate,engagement, totalEngage])
    except:
        break

d.sort(reverse=True)

print(d)

0 个答案:

没有答案