Python 基础: 字典 (dict) 与集合(set)
字典与集合之所以高效的原因是: 内部结构都是一张哈希表.
平均情况下插入, 查找和删除的时间复杂度为 O(1).
查找场景下与列表的性能对比
假设有数量 100,000 的产品列表:
- import time
- id = [x for x in range(0, 100000)]
- price = [x for x in range(200000, 300000)]
- products = list(zip(id, price))
- #products
- # [(0, 200000), (1, 200001)....(99999, 299999)]
要统计出总共有多少种不同的价格, 分别用列表 list 与集合 set 来作为存储的数据结构, 来对比下性能.
用列表作为数据结构:
- # # 计算列表版本的时间
- # list version
- def find_unique_price_using_list(products):
- unique_price_list = []
- for _, price in products: # A
- if price not in unique_price_list: #B
- unique_price_list.append(price)
- return len(unique_price_list)
- start_using_list = time.perf_counter()
- find_unique_price_using_list(products)
- end_using_list = time.perf_counter()
- print("time elapse using list: {}".format(end_using_list - start_using_list))
用集合作为数据结构:
- # # 计算集合版本的时间
- # set version
- def find_unique_price_using_set(products):
- unique_price_set = set()
- for _, price in products:
- unique_price_set.add(price)
- return len(unique_price_set)
- start_using_set = time.perf_counter()
- find_unique_price_using_set(products)
- end_using_set = time.perf_counter()
- print("time elapse using set: {}".format(end_using_set - start_using_set))
从结果可以看出, 性能差异非常大, 使用合适的数据结构非常重要.
Dict 与 Set 基础
集合不支持索引操作
判断元素是否在 dict/set 中用 in 操作符
- dict1 = {
- 'a':1,'b':2
- }
- print('a' in dict1) #True
- print(1 in dict1) #False
- set1 = {
- 'a','b','c'
- }
- print(1 in set1) #False
- print('b' in set1) #True
3. 集合的 pop()方法是随机返回一个元素, 并把集合中的该元素删除
4. 集合与字典的排序
- # 字典排序
- d = {
- 'b': 1, 'a': 2, 'c': 10
- }
- d_sorted_by_key = sorted(d.items(), key=lambda x: x[0]) # 根据字典键的升序排序
- d_sorted_by_value = sorted(d.items(), key=lambda x: x[1]) # 根据字典值的升序排序
- d_sorted_by_key
- [('a', 2), ('b', 1), ('c', 10)]
- d_sorted_by_value
- [('b', 1), ('a', 2), ('c', 10)]
- # 集合排序
- s = {
- 3, 4, 2, 1
- }
- sorted(s) # 对集合的元素进行升序排序
- [1, 2, 3, 4]
参考资料:
极客时间《Python 核心技术与实战》专栏
来源: https://www.2cto.com/kf/201905/809200.html