[英]Removing specific values from dictionary
I am working on problems related to dictionaries and lists.我正在处理与字典和列表相关的问题。
This is my input data这是我的输入数据
{12: [33, 1231, (40, 41), (299, 304), (564, 569), (1139, 1143), (1226, 1228)],
17: [9, 1492, (30, 43), (1369, 1369)],
20: [9, 1492, (30, 45), (295, 310), (561, 573), (927, 929), (1133, 1148), (1222, 1236), (1354, 1356), (1368, 1369)],
26: [34, 1364],
27: [300, 1360, (918, 922)],
36: [34, 1364]}
My objective is to remove key-value pair which contains only integer data type inside the list of values.我的目标是删除值列表中仅包含整数数据类型的键值对。 (In this input data, I want to remove data such as 26: [34, 1364] and 36: [34, 1364]).
(在这个输入数据中,我想删除诸如 26: [34, 1364] 和 36: [34, 1364] 之类的数据)。
So, my output would look like this所以,我的输出看起来像这样
{12: [33, 1231, (40, 41), (299, 304), (564, 569), (1139, 1143), (1226, 1228)],
17: [9, 1492, (30, 43), (1369, 1369)],
20: [9, 1492, (30, 45), (295, 310), (561, 573), (927, 929), (1133, 1148), (1222, 1236), (1354, 1356), (1368, 1369)],
27: [300, 1360, (918, 922)]}
What is the most efficient method to solve this problem.解决这个问题最有效的方法是什么。
Use a dictionary comprehension with not isinstance(..., int)
:使用带有
not isinstance(..., int)
的字典理解:
{k: v for k, v in dct.items() if any(not isinstance(i, int) for i in v)}
Output:输出:
{12: [33, 1231, (40, 41), (299, 304), (564, 569), (1139, 1143), (1226, 1228)],
17: [9, 1492, (30, 43), (1369, 1369)],
20: [9, 1492, (30, 45), (295, 310), (561, 573), (927, 929), (1133, 1148), (1222, 1236), (1354, 1356), (1368, 1369)],
27: [300, 1360, (918, 922)]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.