37,721
社区成员
发帖
与我相关
我的任务
分享
from datetime import datetime
class Data:
def __init__(self, index, first, second, start_date, end_date):
self.index = index
self.first = first
self.second = second
self.start_date = datetime.strptime(start_date, '%Y-%m-%d')
self.end_date = datetime.strptime(end_date, '%Y-%m-%d')
def __gt__(self, other):
return (self.start_date <= other.start_date) and (self.end_date >= other.end_date)
def is_comparable(self, other):
return self.first == other.first and self.second == other.second
def check(params):
tmp_list = []
result = []
is_append = []
for line in params:
tmp_list.append(Data(*line))
for i in range(len(tmp_list)):
for j in range(i, len(tmp_list)):
if i == j:
continue
else:
if tmp_list[i].is_comparable(tmp_list[j]) and tmp_list[i] > tmp_list[j] and (j not in is_append):
result.append(params[j])
is_append.append(j)
return result
if __name__ == '__main__':
the_data = []
with open('a.csv') as f:
for line in f:
the_data.append(line.replace('\n', '').split(','))
print(check(the_data))
from datetime import datetime
datas = [[1, 'a', 'b', '2018-01-01', '2018-03-01'],
[2, 'a', 'b', '2018-01-01', '2018-02-01'],
[3, 'a', 'b', '2018-03-01', '2018-04-01'],
[4, 'a', 'c', '2018-01-01', '2018-03-01'],
[5, 'a', 'c', '2018-04-01', '2018-05-01'],
[6, 'b', 'b', '2018-01-03', '2018-03-01'],
[7, 'b', 'b', '2018-01-01', '2018-02-01'],
[8, 'a', 'b', '2018-03-01', '2018-03-22'],
[9, 'a', 'b', '2018-04-05', '2018-06-22'],
[10, 'a', 'b', '2018-07-01', '2018-07-22']]
class Data:
def __init__(self, index, first, second, start_date, end_date):
self.index = index
self.first = first
self.second = second
self.start_date = datetime.strptime(start_date, '%Y-%m-%d')
self.end_date = datetime.strptime(end_date, '%Y-%m-%d')
def __gt__(self, other):
return (self.start_date <= other.start_date) and (self.end_date >= other.end_date)
def is_comparable(self, other):
return self.first == other.first and self.second == other.second
def check(params):
tmp_list = []
result = []
for line in params:
tmp_list.append(Data(*line))
for i in range(len(tmp_list)):
for j in range(len(tmp_list)):
if i == j:
continue
else:
if tmp_list[i].is_comparable(tmp_list[j]) and tmp_list[i] > tmp_list[j]:
result.append(params[j])
return result
if __name__ == '__main__':
print(check(datas))