+

Python sorted containers | Introduction

Features :

  • Pure-Python
  • Fully documented
  • Comparison comparison (alternatives, runtime, coefficients loads
  • Performance (often faster than C implementations)
  • Compatible API (almost identical to popular blist and rbtree modules)
  • Feature rich (e.g. get the five largest keys in a sorted dict: d.iloc [-5:])
  • Pragmatic design (for example, SortedSet — is a Python set with a SortedList index)

Containers:

  • SortedList
  • SortedDict
  • SortedSet

Installation :

Mac users and Linux can install using pip command:

  sudo pip install sortedcontainers 

SortedList —

A sorted list — is a sorted mutable sequence in which values ​​are stored in sorted order.

Functions to be added and removed elements:

add (value) : A function that takes one element as parameter and inserts it into the list by maintaining sorted order. Runtime Complexity: O (log (n))

update (iterable) : A function that takes an iterable as input and updates the SortedList adding all the values ​​from the iterable Runtime complexity: O (k * log (n)).

clear () : Remove all values ​​from sorted list. Runtime complexity: O (n).

discard (value) : Remove value from sorted list if it is a member. If value is not a member, do nothing. Runtime complexity: O (log (n)).

Below is the implementation of —

# importing libraries

from sortedcontainers import SortedList, SortedSet, SortedDict

  
# initializes a sorted list with parameters
# it takes an iterative parameter.

sorted_list = SortedList ([ 1 , 2 , 3 , 4 ])

 
# initialize the sorted list using the default constructor

sorted_list = SortedList ()

 
# insert values ​​one at a time using add ()

for i in range ( 5 , 0 , - 1 ):

sorted_list.add (i)

 
# prints elements in sorted order

print ( `list after adding 5 elements:` , sorted_list)

  

print ( `list elements are:` , end = ``)

  
# iterate over the sorted list

for i in sorted_list:

  print (i, end = `` )

print ()

 
# remove all elements with clear ()
sorted_list.clear ()

 
# adding items using the update () function

elements = [ 10 , 9 , 8 , 7 , 6 ]

  
sorted_list.update (elements)

  
# prints the updated list sorted order

print ( ` list after updating: ` , sorted_list)

  
# remove a specific element

sorted_list.discard ( 8 )

 

print ( ` list after removing one element: ` , sorted_list )

 
# remove all elements
sorted_list.clear ()

 

print ( `list after removing all elements using clear:` , sorted_list)

Output:

 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) 

SortedSet —

SortedSet — it is a sorted mutable set in which the values ​​are unique and maintained in sorted order. A sorted set uses a set for operations on sets and maintains a sorted list of values. Sorted set values ​​must be hashable and comparable.

Functions for adding and removing elements:

add (value): A function that takes one element as parameter and inserts it into the set by maintaining sorted order. Runtime Complexity: O (log (n))

clear () : Remove all values ​​from sorted set. Runtime complexity: O (n)

discard (value) : Remove value from sorted set if it is a member. If value is not a member, do nothing. Runtime complexity: O (log (n))

# import libraries

from sortedcontainers import SortedList, SortedSet, SortedDict

 
# initializing a sorted set with parameters
# takes an argument as an argument

sorted_set = SortedSet ([ 1 , 1 , 2 , 3 , 4 ])

 
# initialize the sorted set using the default constructor

sorted_set = SortedSet ()

 
# insert values ​​one at a time

for i in range ( 5 , 0 , - 1 ):

sorted_set.add (i)

 

print ( `set after adding elements: ` , sorted_set)

  
# insert duplicate value

sorted_set.add ( 5 )

 

print ( `set after inserting duplicate element:` , sorted_set)

 
# discard element

sorted_set.discard ( 4 )

 

print ( `set after discarding:` , sorted_set)

 
# check membership using the "in" statement

if ( 2 in sorted_set):

print ( `2 is present` )

else :

print ( `2 is not present` )

  

print ( `set elements are:` , end = ``)

 
# iterate over the sorted set

for i in sorted_set:

print (i, end = `` )

print ()

Output:

 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 

SortedDict —

Sorted dict — it is a sorted mutable mapping in which the keys are stored in sorted order. Sorted dict inherits from dict for storing items and maintains a sorted list of keys. Sorted keys must be hashable and comparable.

Functions for adding and removing elements:

setdefault (key, default = None) : Return value for item identified by key in sorted dict. If key is in the sorted dict then return its value. If key is not in the sorted dict then insert key with value default and return default. Runtime Complexity: O (log (n))

clear () : Remove all values ​​from sorted dict. Runtime complexity: O (n)

get (key, default) : Return the value for key if key is in the dictionary, else default.

# importing libraries

from sortedcontainers import SortedList, SortedSet, SortedDict

 
# initializes a sorted word with parameters
# takes as parameter dictionary object

sorted_dict = SortedDict ( { `a` : 1 , `b` : 2 , `c` : 3 })

 
# sorted word initialization

sorted_dict = SortedDict ({ `a` : 1 , `c` : 2 , `b` : 3 })

  
# print dictation

print ( `sorted dict is:` , sorted_dict)

  
# add key = & gt; value pairs

sorted_dict [ `d` ] = 3

  

print ( `sorted dict after adding an element: ` , sorted_dict)

  
# add an element using setdefault ()

sorted_dict.setdefault ( ` e` , 4 )

 

print ( ` sorted dict after setdefault (): ` , sorted_dict)

  
# using get function

print ( `using the get function to print the value of a:` , sorted_dict.get ( `a` , 0 ))

 
# check membership using the "in" operator

if ( `a` in sorted_dict):

print ( `a is present` )

else :

print ( ` a is not present` )

 

print ( `dict elements are:` , end = ``)

 
# iterate over key = & gt; the value in the dictionary

for key in sorted_dict:

print ( `{} - & gt; {}` . format (key, sorted_dict [key]), end = ` ` )

print ()

 
# remove all elements from the dictate
sorted_dict.clear ()

 

print ( `sorted dict after removing all elements:` , sorted_dict)

Output:

 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 - & gt; 1 b - & gt; 3 c - & gt; 2 d - & gt; 3 e - & gt;4 sorted dict after removing all elements: SortedDict (None, 1000, {}) 

Link: http://www.grantjenks.com/docs/sortedcontainers/index.html

Get Solution for free from DataCamp guru