簡體   English   中英

用嵌套的dict鍵值對創建包含項的列表

[英]Create a list with items from a nested dict key value pair

我想創建一個新列表,其中包含來自大型嵌套字典的項目。

這是嵌套字典的一個片段:

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列表中的字典次數相同。 因此,如果新列表上方的代碼段看起來像這樣:

question_date_per_answer = [[1533083248, 1533083248, 1533083248 , 1533083248, 1533083248], [1533083367]]

我需要這個新列表的原因是,我想確定每個answers creation_date及其關聯的問題creation_date (在每個items dict中表示)之間的差異。

這個新列表在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]

但是,這給我留下的清單與answers creation_date的數量不相等。

我無法解決這個問題。
那么,如何創建這樣一個列表,其中問題創建日期的數量等於答案創建日期的數量? (如question_date_per_answer

提前致謝。

您需要遍歷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)

您仍然可以使用列表。
讓我們嘗試從您已經擁有的列表中制作一個數據框-

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

現在,我們將通過df.fillna(method='ffill')用先前的值來填充NaN。
整個代碼段-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM