[英]How to remove a dict from a list of dicts based on whether a value is more than X months old
I have the following code which removes a dict
from a list
of dict
s in Python based on whether the Date
key has a value greater than 180 days ago (ideally needs to be 6 months):我有以下代码,它根据
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]
An example of my JSON is below (just has one entry but could have hundreds):下面是我的 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 was thinking list comprehension but not sure how to parse the string as a date within it.我在考虑列表理解,但不确定如何将字符串解析为其中的日期。
My code does not work correctly if it needs to remove two elements in a row, since it will always increment i
, regardless of whether it just removed the element at index i
.如果我的代码需要连续删除两个元素,我的代码将无法正常工作,因为它总是会递增
i
,无论它是否刚刚删除了索引i
处的元素。 Also, it will keep running until the end of the original length, so if you remove any elements, this code will end with an exception because gp_clinicals_meds_repeat["GpRepeatMedicationsList"][i]
will no longer exist for the later values of i
.此外,它将一直运行到原始长度结束,因此如果您删除任何元素,此代码将以异常结束,因为
gp_clinicals_meds_repeat["GpRepeatMedicationsList"][i]
将不再存在i
的后续值。
Any suggestions?有什么建议么?
You can use a list comprehension with an if
to easily do this.您可以使用带有
if
的列表推导来轻松做到这一点。 I separated the criteria in a function since it may be a bit more complicated.我在一个函数中分离了标准,因为它可能有点复杂。 I also recommend using
pandas.Timestamp
to handle dates, as it is very robust:我还建议使用
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
To get 6 months instead of 180 days, you can use dateutil.relativedelta
s.要获得 6 个月而不是 180 天,您可以使用
dateutil.relativedelta
s。 The is_recent
function can be changed like (you could add a parameter to allow a configurable number of months). 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.