【发布时间】:2018-10-06 10:44:33
【问题描述】:
我想创建一个包含来自大型嵌套字典的项目的新列表。
这是嵌套字典的 sn-p:
AcceptedAnswersPython_combined.json
{
"items": [
{
"answers": [
{
"creation_date": 1533083368,
"is_accepted": false
},
{
"creation_date": 1533083567,
"is_accepted": false
},
{
"creation_date": 1533083754,
"is_accepted": true
},
{
"creation_date": 1533084669,
"is_accepted": false
},
{
"creation_date": 1533089107,
"is_accepted": false
}
],
"creation_date": 1533083248,
"tags": [
"python",
"pandas",
"dataframe"
]
},
{
"answers": [
{
"creation_date": 1533084137,
"is_accepted": true
}
],
"creation_date": 1533083367,
"tags": [
"python",
"binary-search-tree"
]
}
]
}
新列表应包含每个项目的creation_date 与answers 列表中的字典一样多。
所以如果代码 sn-p 上面的新列表应该是这样的:
question_date_per_answer = [[1533083248, 1533083248, 1533083248 , 1533083248, 1533083248], [1533083367]]
我需要这个新列表的原因是我想确定每个 answers creation_date 与其相关问题 creation_date 之间的区别(在每个 items 字典中说明)。
这个新列表在 pandas Dataframe 中应该如下所示:
question creation date answer creation date
0 1533083248 1533083368
1 1533083248 1533083567
2 1533083248 1533083754
3 1533083248 1533084669
4 1533083248 1533089107
5 1533083367 1533084137
我可以像这样遍历所有问题:
items = json.load(open('AcceptedAnswersPython_combined.json'))['items']
question_creation_date = [item['creation_date'] for item in items]
但这给我留下了一个不等于answerscreation_date的数量的列表。
我无法理解这一点。
那么如何创建这样一个列表,其中问题创建日期的数量等于答案创建日期的数量? (如question_date_per_answer)
提前致谢。
【问题讨论】:
-
将
item['creation_date']作为列表乘以您有答案的次数(即[item['creation_date']] * len(item['answers']))。
标签: python python-3.x pandas list dictionary