输出结果
- 输出:
dict_keys(['a', 'b', 'c'])
dict1 = {'a': 1, 'b': 2, 'c': 3}
keys = dict1.keys()
print(keys)
判断dict_keys对象是否包含某个元素
dict1 = {'a': 1, 'b': 2, 'c': 3}
keys = dict1.keys()
if "a" in keys:
print("dict_keys中包含a")
遍历dict_keys
dict1 = {'a': 1, 'b': 2, 'c': 3}
keys = dict1.keys()
for key in keys:
print(key)
将dict_keys对象转化为列表
dict1 = {'a': 1, 'b': 2, 'c': 3}
keys = dict1.keys()
key_list = list(keys)
print(key_list)
求并、交、差集
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'d': 4, 'e': 5, 'a': 6}
keys1 = dict1.keys()
keys2 = dict2.keys()
union = set(keys1) | set(keys2)
intersection = set(keys1) & set(keys2)
difference = set(keys1) - set(keys2)
print("并集:", union)
print("交集:", intersection)
print("差集:", difference)