[英]How to remove a dict from a list of dicts based on whether a value is more than X months old
我有以下代码,它根据Date
键的值是否大于 180 天前(理想情况下需要 6 个月)从 Python 中的dict
list
中删除一个dict
:
gp_clinicals_meds_repeat = session["gp_clinicals_meds_repeat"]
for i in range(len(gp_clinicals_meds_repeat["GpRepeatMedicationsList"])):
date_object = parser.parse(gp_clinicals_meds_repeat["GpRepeatMedicationsList"][i]["Date"])
months_between = datetime.now() - date_object
if months_between.days > 180:
del gp_clinicals_meds_repeat["GpRepeatMedicationsList"][i]
下面是我的 JSON 示例(只有一个条目,但可能有数百个):
{
"GpRepeatMedicationsList": [{
"Constituent": "",
"Date": "2021-07-15T00:00:00",
"Dosage": "0.6ml To Be Taken Each Day",
"LastIssuedDate": "2021-07-15T00:00:00",
"MixtureId": "",
"Quantity": "50",
"ReadCode": "DADR8795BRIDL",
"Rubric": "Dalivit oral drops (Dendron Brands Ltd)",
"TenancyDescription": "Orglinks",
"Units": "ml"
}],
"TotalItemCount": 1
}
我在考虑列表理解,但不确定如何将字符串解析为其中的日期。
如果我的代码需要连续删除两个元素,我的代码将无法正常工作,因为它总是会递增i
,无论它是否刚刚删除了索引i
处的元素。 此外,它将一直运行到原始长度结束,因此如果您删除任何元素,此代码将以异常结束,因为gp_clinicals_meds_repeat["GpRepeatMedicationsList"][i]
将不再存在i
的后续值。
有什么建议么?
您可以使用带有if
的列表推导来轻松做到这一点。 我在一个函数中分离了标准,因为它可能有点复杂。 我还建议使用pandas.Timestamp
来处理日期,因为它非常健壮:
import pandas as pd
def is_recent(entry):
date_object = pd.to_datetime(entry["Date"])
days_between = pd.Timestamp.today() - date_object
return days_between < pd.Timedelta(days=180)
original_clinicals = gp_clinicals_meds_repeat["GpRepeatMedicationsList"]
recent_clinicals = [entry for entry in original_clinicals if is_recent(entry)]
gp_clinicals_meds_repeat["GpRepeatMedicationsList"] = recent_clinicals # Replace the original list
要获得 6 个月而不是 180 天,您可以使用dateutil.relativedelta
s。 is_recent
函数可以更改为(您可以添加一个参数以允许可配置的月数)。
import pandas as pd
import dateutil.relativedelta as relativedelta
def is_recent(entry):
limit_time = pd.Timestamp.today() - relativedelta.relativedelta(months=6)
return pd.to_datetime(entry["Date"]) > limit_time
original_clinicals = gp_clinicals_meds_repeat["GpRepeatMedicationsList"]
recent_clinicals = [entry for entry in original_clinicals if is_recent(entry)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.