Hướng dẫn python ordereddict time complexity - độ phức tạp của thời gian ra lệnh trong python

Bạn có thể kiểm tra triển khai Python thuần túy của OrderedDict.move_to_end(), tương đương với việc triển khai C:

def move_to_end(self, key, last=True):
    '''Move an existing element to the end (or beginning if last is false).
    Raise KeyError if the element does not exist.
    '''
    link = self.__map[key]
    link_prev = link.prev
    link_next = link.next
    soft_link = link_next.prev
    link_prev.next = link_next
    link_next.prev = link_prev
    root = self.__root
    if last:
        last = root.prev
        link.prev = last
        link.next = root
        root.prev = soft_link
        last.next = link
    else:
        first = root.next
        link.prev = root
        link.next = first
        first.prev = soft_link
        root.next = link

Về cơ bản, phương pháp này tìm kiếm một liên kết trong một danh sách được liên kết trong từ điển self.__map và cập nhật các gợi ý trước và tiếp theo cho liên kết và các hàng xóm của nó. Vì tất cả các hoạt động trên mất thời gian liên tục, sự phức tạp của OrderedDict.move_to_end() cũng không đổi.
Since all of the operations above take constant time, the complexity of OrderedDict.move_to_end() is constant as well.

Một đơn đặt hàng, một lệnh ghi nhớ thứ tự trong các khóa đó lần đầu tiên được chèn. Nếu một mục mới ghi đè lên một mục nhập hiện có, vị trí chèn ban đầu không thay đổi. Xóa một mục nhập và tái cấu trúc nó sẽ di chuyển nó đến cuối cùng. Từ điển được đặt hàng bằng cách nào đó có thể được sử dụng ở nơi có việc sử dụng bản đồ băm và hàng đợi. Nó có đặc điểm của cả hai thành một. Giống như hàng đợi, nó nhớ thứ tự và nó cũng cho phép chèn và xóa ở cả hai đầu. Và giống như một từ điển, nó cũng hoạt động như một bản đồ băm. & Nbsp;OrderedDictis a dict that remembers the order in that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end. Ordered dictionary somehow can be used in the place where there is a use of hash Map and queue. It has characteristics of both into one. Like queue, it remembers the order and it also allows insertion and deletion at both ends. And like a dictionary, it also behaves like a hash map. 

Lưu ý: Từ Python 3.6 trở đi, thứ tự được giữ lại cho các đối số từ khóa được chuyển cho hàm tạo đơn hàng, tham khảo PEP-468. & NBSP; From Python 3.6 onwards, the order is retained for keyword arguments passed to the OrderedDict constructor, refer to PEP-468. 

Phương pháp từ điển đặt hàng

Hãy cùng xem xét các phương pháp khác nhau được cung cấp bởi từ điển được đặt hàng. & NBSP;

  • popitem():

Phương pháp này được sử dụng để xóa một khóa từ đầu.

Syntax:

popitem(last = True)                      

Nếu cuối cùng là sai thì phương pháp này sẽ xóa một khóa từ đầu từ điển. Điều này đóng vai trò là FIFO (đầu tiên trong lần đầu tiên) trong hàng đợi nếu không, phương thức nó sẽ xóa khóa từ cuối từ điển.

Độ phức tạp về thời gian: O (1).O(1).

Để hiểu rõ hơn, hãy xem mã.

Python3

from collections import OrderedDict

popitem(last = True)                      
0____11
popitem(last = True)                      
2
popitem(last = True)                      
3
popitem(last = True)                      
4

popitem(last = True)                      
5
popitem(last = True)                      
6
popitem(last = True)                      
7
popitem(last = True)                      
4

popitem(last = True)                      
5
move_to_end(key, last = True)
0

move_to_end(key, last = True)
1

popitem(last = True)                      
5
popitem(last = True)                      
6
move_to_end(key, last = True)
4
popitem(last = True)                      
4

popitem(last = True)                      
5
move_to_end(key, last = True)
0

popitem(last = True)                      
5
popitem(last = True)                      
6
popitem(last = True)                      
7
popitem(last = True)                      
4

popitem(last = True)                      
5
popitem(last = True)                      
6OrderedDict.move_to_end()4
popitem(last = True)                      
4

popitem(last = True)                      
5
move_to_end(key, last = True)
0

Output:

OrderedDict.move_to_end()3from3from4

popitem(last = True)                      
1 OrderedDict.move_to_end()0
popitem(last = True)                      
4
OrderedDict([(‘G’, None), (‘e’, None), (‘k’, None), (‘s’, None), (‘F’, None), (‘o’, None), (‘r’, None)])
After Deleting Last item : 
OrderedDict([(‘G’, None), (‘e’, None), (‘k’, None), (‘s’, None), (‘F’, None), (‘o’, None)])
After Deleting Key from Beginning : 
OrderedDict([(‘e’, None), (‘k’, None), (‘s’, None), (‘F’, None), (‘o’, None)])

  • move_to_end():

Phương pháp này được sử dụng để di chuyển một khóa hiện có của từ điển đến cuối hoặc đến đầu. Có hai phiên bản của chức năng này - & nbsp;

Syntax:

move_to_end(key, last = True)

Nếu cuối cùng là đúng thì phương pháp này sẽ di chuyển một khóa hiện có của từ điển cuối cùng nếu không nó sẽ di chuyển một khóa hiện có của từ điển ngay từ đầu. Nếu khóa được di chuyển vào đầu thì nó đóng vai trò là FIFO (đầu tiên trong lần đầu tiên) trong một hàng đợi.

Độ phức tạp về thời gian: O (1) & NBSP;O(1) 

Python3

from collections import OrderedDict

popitem(last = True)                      
0____11
popitem(last = True)                      
2
popitem(last = True)                      
3
popitem(last = True)                      
4

popitem(last = True)                      
5
popitem(last = True)                      
6
popitem(last = True)                      
7
popitem(last = True)                      
4

popitem(last = True)                      
5
move_to_end(key, last = True)
0

OrderedDict.move_to_end()3OrderedDict.move_to_end()4

popitem(last = True)                      
4

popitem(last = True)                      
5
popitem(last = True)                      
6OrderedDict.move_to_end()8
popitem(last = True)                      
4

popitem(last = True)                      
5
move_to_end(key, last = True)
0

OrderedDict.move_to_end()3from3from4

popitem(last = True)                      
1 OrderedDict.move_to_end()0
popitem(last = True)                      
4

popitem(last = True)                      
5
popitem(last = True)                      
6collections 0
popitem(last = True)                      
4

popitem(last = True)                      
5
move_to_end(key, last = True)
0

Output:

Từ điển gốc & nbsp; OrderedDict ([('g', none), ('e', none), ('k', none), ('s', none), ('f', none), ('o' , Không có), ('r', none)]) sau khi di chuyển khóa 'g' đến cuối từ điển: & nbsp; strongeddict ([('e', none), ('k', none), ('s', Không), ('f', none), ('o', none), ('r', none), ('g', none)]) sau khi di chuyển khóa ngay từ đầu: & nbsp; struneddict (('(' k ', không có), (' e ', none), (' s ', none), (' f ', none), (' o ', none), (' r ', none), (' g ' , Không có)])
OrderedDict([(‘G’, None), (‘e’, None), (‘k’, None), (‘s’, None), (‘F’, None), (‘o’, None), (‘r’, None)])
After moving key ‘G’ to end of dictionary : 
OrderedDict([(‘e’, None), (‘k’, None), (‘s’, None), (‘F’, None), (‘o’, None), (‘r’, None), (‘G’, None)])
After moving Key in the Beginning : 
OrderedDict([(‘k’, None), (‘e’, None), (‘s’, None), (‘F’, None), (‘o’, None), (‘r’, None), (‘G’, None)])

Hoạt động của hàm di chuyển_to_end ()

Về cơ bản, phương pháp này tìm kiếm một liên kết trong một danh sách được liên kết trong bản đồ từ điển .__ và cập nhật các con trỏ trước và tiếp theo cho liên kết và hàng xóm của nó.Nó xóa phần tử đó khỏi vị trí của nó và thêm nó vào cuối hoặc bắt đầu tùy thuộc vào giá trị tham số.Vì tất cả các hoạt động dưới đây mất thời gian liên tục, sự phức tạp của OrderedDict.Move_TO_END () & NBSP; cũng không đổi. & NBSP;