📜  Python排序容器 |一个介绍

📅  最后修改于: 2022-05-13 01:54:25.300000             🧑  作者: Mango

Python排序容器 |一个介绍

Sorted Containers是一个 Apache2 许可的排序集合库,用纯 Python 编写,和 C 扩展一样快。它由 Grant Jenks 创建,是一个开源库。它是容器的集合,允许我们非常有效地插入和删除元素,同时保持排序顺序。

特征:

  • 纯 Python
  • 完整记录
  • 基准比较(替代方案、运行时、负载因子
  • 性能(通常比 C 实现更快)
  • 兼容的 API(几乎与流行的 blist 和 rbtree 模块相同)
  • 功能丰富(例如,获取排序字典中最大的五个键:d.iloc[-5:])
  • 务实的设计(例如,SortedSet 是一个带有 SortedList 索引的Python集)

容器:

  • 排序列表
  • 排序字典
  • 排序集

安装:

MacLinux用户可以通过 pip 命令安装:

sudo pip install sortedcontainers 

排序列表 –

排序列表是排序的可变序列,其中值按排序顺序维护。

添加和删除元素的功能:

下面是实现——

# importing libraries
from sortedcontainers import SortedList, SortedSet, SortedDict
  
# initializing a sorted list with parameters
# it takes an iterable as a parameter.
sorted_list = SortedList([1, 2, 3, 4])
  
# initializing a sorted list using default constructor
sorted_list = SortedList()
  
# inserting values one by one using add()
for i in range(5, 0, -1):
    sorted_list.add(i)
  
# prints the elements in sorted order
print('list after adding 5 elements: ', sorted_list)
  
print('list elements are: ', end = '')
  
# iterating through a sorted list
for i in sorted_list:
    print(i, end = ' ')
print()
  
# removing all elements using clear()
sorted_list.clear()
  
# adding elements using the update() function
elements = [10, 9, 8, 7, 6]
  
sorted_list.update(elements)
  
# prints the updated list in sorted order
print('list after updating: ', sorted_list)
  
# removing a particular element
sorted_list.discard(8)
  
print('list after removing one element: ', sorted_list)
  
# removing all elements
sorted_list.clear()
  
print('list after removing all elements using clear: ', sorted_list)

输出 :

list after adding 5 elements:  SortedList([1, 2, 3, 4, 5], load=1000)

list elements are: 1 2 3 4 5 

list after updating:  SortedList([6, 7, 8, 9, 10], load=1000)

list after removing one element:  SortedList([6, 7, 9, 10], load=1000)

list after removing all elements using clear:  SortedList([], load=1000)

排序集 –

排序集是一个排序的可变集合,其中值是唯一的并按排序顺序维护。排序集使用集合进行集合操作并维护一个排序的值列表。排序后的集合值必须是可散列的和可比较的。

添加和删除元素的功能:

# importing libraries
from sortedcontainers import SortedList, SortedSet, SortedDict
  
# initializing a sorted set with parameters
# it takes an iterable as an argument
sorted_set = SortedSet([1, 1, 2, 3, 4])
  
# initializing a sorted set using default constructor
sorted_set = SortedSet()
  
# inserting values one by one
for i in range(5, 0, -1):
    sorted_set.add(i)
  
print('set after adding elements: ', sorted_set)
  
# inserting duplicate value
sorted_set.add(5)
  
print('set after inserting duplicate element: ', sorted_set)
  
# discarding an element
sorted_set.discard(4)
  
print('set after discarding: ', sorted_set)
  
# checking membership using 'in' operator
if(2 in sorted_set):
    print('2 is present')
else:
    print('2 is not present')
  
print('set elements are: ', end = '')
  
# iterating through a sorted set
for i in sorted_set:
    print(i, end = ' ')
print()

输出 :

set after adding elements:  SortedSet([1, 2, 3, 4, 5], key=None, load=1000)

set after inserting duplicate element:  SortedSet([1, 2, 3, 4, 5], key=None, load=1000)

set after discarding:  SortedSet([1, 2, 3, 5], key=None, load=1000)

2 is present

set elements are: 1 2 3 5

排序字典 –

排序字典是排序的可变映射,其中键以排序顺序维护。 Sorted dict 继承自 dict 以存储项目并维护一个排序的键列表。排序的 dict 键必须是可散列的和可比较的。

添加和删除元素的功能:

# importing libraries
from sortedcontainers import SortedList, SortedSet, SortedDict
  
# initializing a sorted dict with parameters
# it takes a dictionary object as a parameter
sorted_dict = SortedDict({'a': 1, 'b': 2, 'c': 3})
  
# initializing a sorted dict
sorted_dict = SortedDict({'a': 1, 'c': 2, 'b':3})
  
# print the dict
print('sorted dict is: ', sorted_dict)
  
# adding key => value pairs
sorted_dict['d'] = 3
  
print('sorted dict after adding an element: ', sorted_dict)
  
# adding element using setdefault()
sorted_dict.setdefault('e', 4)
  
print('sorted dict after setdefault(): ', sorted_dict)
  
# using the get function
print('using the get function to print the value of a: ', sorted_dict.get('a', 0))
  
# checking membership using 'in' operator
if('a' in sorted_dict):
    print('a is present')
else:
    print('a is not present')
  
print('dict elements are: ', end = '')
  
# iterating over key => value pairs in a dictionary
for key in sorted_dict:
    print('{} -> {}'.format(key, sorted_dict[key]), end = ' ')
print()
  
# removing all elements from the dict
sorted_dict.clear()
  
print('sorted dict after removing all elements: ', sorted_dict)

输出 :

sorted dict is:  SortedDict(None, 1000, {'a': 1, 'b': 3, 'c': 2})

sorted dict after adding an element:  SortedDict(None, 1000, {'a': 1, 'b': 3, 'c': 2, 'd': 3})

sorted dict after setdefault():  SortedDict(None, 1000, {'a': 1, 'b': 3, 'c': 2, 'd': 3, 'e': 4})

using the get function to print the value of a:  1

a is present

dict elements are: a -> 1 b -> 3 c -> 2 d -> 3 e -> 4 

sorted dict after removing all elements:  SortedDict(None, 1000, {})

参考:http://www.grantjenks.com/docs/sortedcontainers/index.html