我怎么知道哪个人住了最多的夜晚?姓名和总共多少天? (日期格式MM / DD)
例如
文本文件包含
Robin 01/11 01/15
Mike 02/10 02/12
John 01/15 02/15
预期输出
('john', 30 )
我的代码
def longest_stay(fpath):
with open(fpath,'r')as f_handle:
stay=[]
for line in f_handle:
name, a_date, d_date = line.strip().split()
diff = datetime.strptime(d_date, "%m/%d") -datetime.strptime(a_date, "%m/%d")
stay.append(abs(diff.days+1))
return name,max(stay)
它总是返回名字。
答案 0 :(得分:1)
这也可以使用pandas实现。我认为使用熊猫会更简单。
我发现的一个问题是,当你有很多人入住最多的夜晚时,你想要怎样处理。我在下面的代码中解决了这个问题。
import pandas as pd
from datetime import datetime as dt
def longest_stay(fpath):
# Reads the text file as Dataframe
data = pd.read_csv(fpath + 'test.txt', sep=" ", header = None)
# adding column names to the Data frame
data.columns = ['Name', 'a_date', 'd_date']
# Calculating the nights for each customer
data['nights'] = datetime.strptime(d_date, "%m/%d") - datetime.strptime(a_date, "%m/%d")
# Slicing the data frame by applying the condition and getting the Name of the customer and nights as a tuple (as expected)
longest_stay = tuple( data.ix[data.nights == data.nights.max(), {'Name', 'nights'}])
# In case if many stayed for the longest night. Returns a list of tuples.
longest_stay = [tuple(x) for x in longest_stay]
return longest_stay
答案 1 :(得分:0)
您的代码失败但没有存储名字,这是因为name
将被设置为文件中的姓氏,因为您只存储了日期,因此您总是看到姓氏
您还添加+ 1,这似乎不正确,因为您不应该添加或包括最后一天,因为该人不会在那晚停留。您的代码实际上会偶然输出('John', 32)
正确的名称,因为它是示例文件中的最后一个,并且假日为1。
只需跟踪最佳状态,包括姓名和日期计数,并将剩余时间作为衡量标准并在结束时返回:
from datetime import datetime
from csv import reader
def longest_stay(fpath):
with open(fpath,'r')as f_handle:
mx,best = None, None
for name, a_date, d_date in reader(f_handle,delimiter=" "):
days = (datetime.strptime(d_date, "%m/%d") - datetime.strptime(a_date, "%m/%d")).days
# first iteration or we found
if best is None or mx < days:
best = name, days
return best
Outout:
In [13]: cat test.txt
Robin 01/11 01/15
Mike 02/10 02/12
John 01/15 02/15
In [14]: longest_stay("test.txt")
# 31 days not including the last day as a stay
Out[14]: ('John', 31)
如果格式并不总是采用格式start-end,你只需要使用abs,但是如果日期有多年,请注意使用abs值可能会得到错误的输出。