从字典中识别类别并进行打印,可以通过遍历字典的键值对来实现。以下是一个示例代码,可以识别字典中不同类别的值,并将其打印出来:
def print_categories(dictionary):
categories = set()
for key, value in dictionary.items():
categories.add(type(value).__name__)
for category in categories:
print(category)
# 示例字典
my_dictionary = {
"name": "John Doe",
"age": 30,
"salary": 50000.00,
"is_employed": True,
"skills": ["Python", "Java", "C++"],
"address": {
"street": "123 Main St",
"city": "New York",
"country": "USA"
}
}
print_categories(my_dictionary)
运行以上代码,将会输出字典中值的不同类别:
str
bool
list
float
dict
int
在这个例子中,我们遍历了字典的键值对,将每个值的类型的类名加入到一个集合中。然后,我们遍历这个集合并打印每个类名,即为字典中值的不同类别。
注意:该代码只能识别内置数据类型和一些常见的数据结构类别,对于自定义类或第三方库提供的类,需要根据具体情况进行额外的处理。
领取专属 10元无门槛券
手把手带您无忧上云