根据列表中的元素数过滤嵌套字典

时间:2020-06-15 09:09:55

标签: python dictionary

如何根据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')]}}

所需的输出:

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')]}}

1 个答案:

答案 0 :(得分:0)

尝试一下:

如果内部字典只有一个键,它将起作用

res = {k: v for k, v in d.items() if len(list(v.values())[0]) > 2}
print(res)

输出:

{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')]]}}