提问人:Tanya Gaychenkova 提问时间:10/26/2023 最后编辑:Tanya Gaychenkova 更新时间:10/26/2023 访问量:77
如何组合该人的旅行日期?
How to combine dates of trips of the person?
问:
你能帮帮我吗?我有一个算法,它应该找到这个人最近旅行的到达日期和离开日期。 在这里:
trips = dict()
countries = dict()
passengers = dict()
for row in df.itertuples(index=False):
party_rk, passenger, created_dt, arrival_dt, departure_dt, country_cd = row
# if the person doesn't have any trips then add the first one
if trips.get(party_rk, 'Empty') == 'Empty':
trips[party_rk] = []
trips[party_rk].append([created_dt, arrival_dt, departure_dt])
countries[party_rk] = []
countries[party_rk].append(set(country_cd))
passengers[party_rk] = []
passengers[party_rk].append(set([tuple(passenger)]))
else:
trips_by_party = trips[party_rk]
end_last_trip = trips_by_party[-1][1]
previous_country_cd = countries[party_rk][-1]
if (country_cd == 'RUS' and previous_country_cd == 'RUS') or (country_cd != 'RUS' and previous_country_cd != 'RUS'):
# if the next trip starts earlier or just after the last one then combine them
if arrival_dt <= end_last_trip + datetime.timedelta(days=1):
trips[party_rk][-1][1] = max(end_last_trip, departure_dt)
countries[party_rk][-1].update(set(country_cd))
add_person(passengers[party_rk][-1], passenger)
# add new period of the trip
else:
trips[party_rk].append([created_dt, arrival_dt, departure_dt])
countries[party_rk].append(set(country_cd))
passengers[party_rk].append(set([tuple(passenger)]))
else:
trips[party_rk].append([created_dt, arrival_dt, departure_dt])
countries[party_rk].append(set(country_cd))
passengers[party_rk].append(set([tuple(passenger)]))
下面是示例:
它应该是这样的:
1次旅行: 2023-11-04 - 2023-11-11
2次旅行: 2024-04-13 - 2024-04-20
主要思想是将彼此接近的旅行结合起来,并将它们与比以前的旅行晚得多的旅行分开。
答: 暂无答案
评论