【发布时间】:2021-08-03 16:08:32
【问题描述】:
如何准确检查字典中的值类型是否为列表类型
我有一个列表,需要检查字典“test_dict”中的键是否存在,并知道是否有任何值是“列表”类型
col_list = ['pat_cd','dsply_nm', 'opt_cd','dsply_val']
test_dict={'pat_cd':'123','opt_cd':['232','245'],'test':['123','1232']}
result=type(any(test_dict[i]) for i in col_list if i in test_dict) is list
print(result)
##
Output
False
输出返回 'False'.. 理想情况下它应该返回 'True',因为 'opt_cd' 的值是列表类型
谁能帮忙解决这个问题?
谢谢
【问题讨论】:
-
但是键
test不在col_list中,所以应该检查它..? -
any(isinstance(test_dict[i], list) for i in col_list if i in test_dict)? -
@alex,我已经更新了这个问题。只有那些反对 col_list 的人需要检查它是否是它的列表类型
-
Best way to check if a variable is a list 和 What are the differences between type and instance 涵盖了如何 - 你的问题是任何不正确的使用。
标签: python-3.x dictionary