[英]Filter nested dictionary based on number of elements in list
如何根據 ID 必須包含三個或更多時間戳的條件過濾以下字典?
dict={10009: {196962305: [Timestamp('2019-12-27 03:06:50')]},
10051: {2854032: [Timestamp('2019-12-27 19:16:11')],
48600461: [Timestamp('2019-12-29 01:56:19')]},
10061: {62464559: [Timestamp('2019-12-31 02:58:48'), Timestamp('2019-12-30 21:35:15'), Timestamp('2019-12-28 09:27:55'), [Timestamp('2019-12-28 12:05:32')]}}
所需的 output:
new_dict={10061: {62464559: [Timestamp('2019-12-31 02:58:48'), Timestamp('2019-12-30 21:35:15'), Timestamp('2019-12-28 09:27:55'), [Timestamp('2019-12-28 12:05:32')]}}
嘗試這個:
如果內部字典只有一個鍵,它將起作用
res = {k: v for k, v in d.items() if len(list(v.values())[0]) > 2}
print(res)
Output:
{10061: {62464559: [Timestamp('2019-12-31 02:58:48'), Timestamp('2019-12-30 21:35:15'), Timestamp('2019-12-28 09:27:55'), [Timestamp('2019-12-28 12:05:32')]]}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.