【问题标题】:Create a list with items from a nested dict key value pair使用嵌套字典键值对中的项目创建列表
【发布时间】: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_dateanswers 列表中的字典一样多。 所以如果代码 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


【解决方案1】:

您需要遍历 item["answers"] 然后为 oreder 中的每个答案获取 creation_date 以获得答案创建日期。

my_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"
    ]
    }
]
}"""

import json

data = json.loads(my_json)
dates = [(question["creation_date"], answer["creation_date"])
         for question in data["items"] for answer in question["answers"]]
print(dates)

【讨论】:

  • 谢谢,这成功了:)。我稍微修改了日期,但效果很好。
【解决方案2】:

您仍然可以使用手头的列表。
让我们尝试从您已经拥有的列表中创建一个数据框 -

l = [[1533083248, 1533083248, 1533083248 , 1533083248, 1533083248], [1533083367]]
df = pd.DataFrame(l)

不幸的是,你得到了以下-

0   1   2   3   4
0   1533083248  1.533083e+09    1.533083e+09    1.533083e+09    1.533083e+09
1   1533083367  NaN     NaN     NaN     NaN

所以我们需要转置它。为此,让我们执行以下操作 -

from itertools import zip_longest
k = list(list(zip_longest(*l))) #Unless the list will be truncated to the length of shortest list.
df = pd.DataFrame(k)

输出-

0   1
0   1533083248  1.533083e+09
1   1533083248  NaN
2   1533083248  NaN
3   1533083248  NaN
4   1533083248  NaN

现在我们将用之前的值向前填充 NaN - df.fillna(method='ffill')
整个sn-p -

from itertools import zip_longest
l=[1533083248, 1533083248, 1533083248 , 1533083248, 1533083248], [1533083367]
k=list(list(zip_longest(*l)))
df = pd.DataFrame(k)
df.fillna(method='ffill')

瞧-

    0   1
0   1533083248  1.533083e+09
1   1533083248  1.533083e+09
2   1533083248  1.533083e+09
3   1533083248  1.533083e+09
4   1533083248  1.533083e+09

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多