Python字典:从“乱点鸳鸯谱”到“秩序井然”的键值对艺术
传说Python 3.6之前,字典是个随性的媒人,乱点鸳鸯谱;3.7之后,它成了讲究先来后到的月老,牵线必有顺序。
一、字典的前世今生:从无序到有序
在Python 3.6及之前,字典就像个喝醉的图书管理员——你永远不知道他下次会把书(值)放在哪个书架(内存位置)上。但从Python 3.7开始,字典保持了元素插入的顺序,这让很多开发者感动得热泪盈眶:
ordered_dict = {'苹果': 5, '香蕉': 3, '橙子': 7} print(list(ordered_dict.keys()))
for fruit, count in ordered_dict.items(): print(f"{fruit}: {count}个")
|
二、字典的五种“出生方式”
1. 最直白的方式:花括号大法
person = {"name": "小明", "age": 22, "city": "北京"}
valid_keys = { "string_key": "OK", 123: "OK", ("元", "组"): "OK", }
|
2. 最像函数调用的方式:dict()构造函数
config = dict(name="static", age=22, language="Python") print(config)
|
3. 最结构化的方式:列表+元组
student_data = [("学号", "1001"), ("姓名", "李华"), ("成绩", 95)] student = dict(student_data) print(student)
|
4. 最“拉链”的方式:zip()函数
keys = ["姓名", "职业", "等级"] values = ["孙悟空", "齐天大圣", 999] character = dict(zip(keys, values)) print(character)
|
5. 最“批量生产”的方式:fromkeys()
exam_scores = dict.fromkeys(["语文", "数学", "英语"], 60) print(exam_scores)
empty_dict = dict.fromkeys(["a", "b", "c"]) print(empty_dict)
|
三、字典的“增删改查”七十二变
让我们用一个例子贯穿始终:
d = dict.fromkeys("python", 100) print("初始字典:", d)
|
📈 增:字典的“人口增长”
d['Y'] = 10 d['y'] = 101 print("新增后:", d)
|
🗑️ 删:字典的“瘦身计划”
removed_value = d.pop('p', "没有这个键") print(f"删除p键,返回值: {removed_value}")
not_exist = d.pop('N', "没有这个键") print(f"删除不存在的N键: {not_exist}")
last_item = d.popitem() print(f"删除最后一项: {last_item}") print("删除后字典:", d)
del d['t']
|
✏️ 改:字典的“变形记”
d['h'] = 999 print("修改h后:", d)
d.update({'o': 888, 'n': 777}) d.update(y=666, Y=555) print("批量更新后:", d)
|
🔍 查:字典的“寻宝游戏”
value = d.get('x', "没有这个键") print(f"获取不存在的键x: {value}")
new_value = d.setdefault('x', 300) print(f"setdefault后x的值: {new_value}") print("当前字典:", d)
|
📋 其他实用操作
d_copy = d.copy() d_copy['y'] = "被修改" print("原字典:", d) print("拷贝字典:", d_copy)
print("键视图:", d.keys()) print("值视图:", d.values()) print("键值对视图:", d.items())
print("\n遍历字典:") for key, value in d.items(): print(f" {key}: {value}")
|
四、字典的“十八般武艺”
d = dict.fromkeys("python", 100)
print(f"字典长度: {len(d)}")
print(f"'p'在字典中吗: {'p' in d}") print(f"'z'不在字典中吗: {'z' not in d}")
keys_list = list(d) print(f"所有键的列表: {keys_list}")
key_iterator = iter(d) print("迭代键:", end=" ") for key in key_iterator: print(key, end=" ") print()
print(f"排序后的键: {sorted(d)}") print(f"反转后的键: {list(reversed(d))}")
matrix_dict = { "python": {1: "one", 2: "two"}, "golang": {"hello": "world"}, "data": [1, 2, 3] } print(f"访问嵌套字典: {matrix_dict['python'][2]}")
original = {'a': 10, 'b': 20, 'c': 5, 'd': 30} deduce_dict = {k: v*2 for k, v in original.items() if v > 10} print(f"字典推导式结果: {deduce_dict}")
word = "hello" char_count = {char: word.count(char) for char in set(word)} print(f"字符计数字典: {char_count}")
|
五、字典使用小贴士
- 键的唯一性:字典的键是唯一的,后插入的会覆盖先插入的
- 哈希要求:键必须是可哈希的(不可变类型)
- 性能优势:字典的查找、插入、删除操作平均时间复杂度是O(1)
- 内存考虑:字典比列表占用更多内存,但访问速度更快
- 3.7+的特性:记住,Python 3.7+中字典保持插入顺序,但这不是“排序”!
六、实战:用字典解决实际问题
text = "python is great python is fun python is powerful" words = text.split()
word_count = {} for word in words: if word in word_count: word_count[word] += 1 else: word_count[word] = 1
word_count2 = {} for word in words: word_count2[word] = word_count2.get(word, 0) + 1
from collections import Counter word_count3 = Counter(words)
print("单词频率:", word_count2)
|
总结
Python字典就像一个智能的、有序的键值对管家,从3.7版本开始,它记住了每个元素到来的顺序。无论是快速查找、数据映射还是配置存储,字典都是Python程序员手中不可或缺的利器。
记住这些要点,你的字典技能就能从”青铜”升级到”王者”:
- ✅ 字典是有序的(Python 3.7+)
- ✅ 键必须是不可变类型
- ✅ 掌握五种创建方式
- ✅ 熟练使用增删改查方法
- ✅ 善用视图对象和推导式
现在,去用字典编织你的代码世界吧!它会让你的程序更加优雅、高效。✨
PS:字典虽好,可不要贪杯哦~ 当数据量超大时,记得考虑内存使用;当需要频繁排序时,可以考虑其他数据结构。编程之道,在于选择合适的工具解决具体问题。