对于python-noob问题很抱歉,我需要在我的家庭作业问题上寻求一些帮助,即试图遍历输入文件并创建具有新键值名称的列表。
我从文件中读取了一些数据:
{
"1": [
{
"id": 1,
"date": "2019-06-19T12:37:10.599Z",
"name": "In"
},
{
"id": 1,
"date": "2019-06-19T10:40:36.233Z",
"name": "Out"
},
{
"id": 1,
"date": "2019-06-19T06:25:21.473Z",
"name": "In"
},
{
"id": 1,
"date": "2019-06-04T08:12:23.795Z",
"name": "Out"
}
],
"2": [
{
"id": 2,
"date": "2019-06-19T12:37:10.599Z",
"name": "In"
},
{
"id": 2,
"date": "2019-06-19T10:40:36.233Z",
"name": "Out"
}
]
}
我读入了这些数据,然后尝试使用以下代码:
data = []
output_data = []
for item in full_data:
if full_data.get('name') == 'In':
li_time = sorted(full_data, key=lambda x: (x['date'], '%Y-%m-%dT%H:%M:%S.%fZ'), reverse=True).pop()
if full_data.get('name') == 'In':
fi_time = sorted(full_data, key=lambda x: (x['date'], '%Y-%m-%dT%H:%M:%S.%fZ')).pop()
if full_data.get('name') == 'Out':
lo_time = sorted(full_data, key=lambda x: (x['date'], '%Y-%m-%dT%H:%M:%S.%fZ'), reverse=True).pop()
data = {
'id': full_data.get('id'),
'first_in_time': fi_time,
'last_in_time': li_time,
'last_out_time': lo_time,
}
output_data.append(data)
我的想法是遍历full_data,并为每个项目使用pop()获得每个id的最早的“进入”时间,最新的“进入”时间和最新的“退出”时间。
这将给我以下输出数据:
{
"id": 1,
"first_in_time": "2019-06-19T06:25:21.473Z",
"last_in_time": "2019-06-19T12:37:10.599Z",
"last_out_time": "2019-06-19T10:40:36.233Z",
},
{
"id": 2,
"first_in_time": "2019-06-19T12:37:10.599Z",
"last_in_time": "2019-06-19T12:37:10.599Z",
"last_out_time": "2019-06-19T10:40:36.233Z",
}
但是,两者都没有接full_data.get('id')
,并且名称没有定义。当我尝试定义它们时,新数据只是空的,表明我的循环不正确。
答案 0 :(得分:0)
鉴于full_data
包含上面给出的结构,对其进行迭代将为您提供第一级的 key :
for item in full_data:
# item will be "1"
..
# item will be "2"
这与您的代码不符,因为您可能还想遍历内部结构:
for k in full_data:
# full_data[k] is however a list of in/out entries:
for item in full_data[k]:
# item is now {"id": "1", "date": ..., ... }
不过,可能还有一种更具可读性的方式:
for k in full_data:
# fetch the in and out values to a list with just the dates
in_values = [x['date'] for x in full_data[k] if x['name'] == 'In']
out_values = [x['date'] for x in full_data[k] if x['name'] == 'Out']
output_data.append({
"id": k,
"first_in_time": min(in_values),
"last_in_time": max(in_values),
"last_out_time": max(out_values),
})