Hướng dẫn reverse ordereddict python - lệnh ngược lại python

Nếu bạn đang sử dụng Python3, bạn không nhất thiết phải tạo một từ điển mới.don't necessarily have to create a new dictionary.don't necessarily have to create a new dictionary.

Nội dung chính ShowShow

  • Python OrderedDict
  • Python OrderedDict Examples
  • Creating OrderedDict object
  • Adding, Replacing, Removing items from OrderedDict
  • OrderedDict move_to_end example
  • OrderedDict popitem example
  • OrderedDict Reverse Iteration
  • OrderedDict Equality Tests Example
  • Kết thúc
  • Đọc liên quan

Bạn có thể sử dụng

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
7:

Di chuyển một khóa hiện có đến một trong hai đầu của một từ điển được đặt hàng. Mục được di chuyển sang đầu bên phải nếu cuối cùng là đúng [mặc định] hoặc đến đầu nếu cuối cùng là sai. Tăng KeyError nếu khóa không tồn tại

Ví dụ [di chuyển

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
8 để bắt đầu từ điển để đạt được đầu ra của bạn]:
>>> d = OrderedDict[]
>>> for c in 'ABCDE':
...     d[c.lower[]] = c
...
>>> d
OrderedDict[[['a', 'A'], ['b', 'B'], ['c', 'C'], ['d', 'D'], ['e', 'E']]]
>>> d.move_to_end['e',last=False] # last=False moves to beginning
>>> d
OrderedDict[[['e', 'E'], ['a', 'A'], ['b', 'B'], ['c', 'C'], ['d', 'D']]]

Python OrderedDict is a dict subclass that maintains the items insertion order. When we iterate over an OrderedDict, items are returned in the order they were inserted. A regular dictionary doesn’t track the insertion order. So when iterating over it, items are returned in an arbitrary order. When we want to make sure that items are returned in the order they were inserted, we can use OrderedDict.

Nội dung chính

  • Python OrderedDict
  • Python OrderedDict Examples
  • Creating OrderedDict object
  • Adding, Replacing, Removing items from OrderedDict
  • OrderedDict move_to_end example
  • OrderedDict popitem example
  • OrderedDict Reverse Iteration
  • OrderedDict Equality Tests Example
  • Kết thúc
  • Đọc liên quan

Python OrderedDict

  • Python OrderedDict Examples
  • Creating OrderedDict object
  • Adding, Replacing, Removing items from OrderedDict
  • OrderedDict move_to_end example
  • OrderedDict popitem exampleFIFO order. It accepts a boolean argument
    OrderedDict[]
    OrderedDict[[['kiwi', 4], ['apple', 5], ['cat', 3]]]
    
    1, if it’s set to
    OrderedDict[]
    OrderedDict[[['kiwi', 4], ['apple', 5], ['cat', 3]]]
    
    2 then items are returned in LIFO order.
  • OrderedDict Reverse Iteration
  • OrderedDict Equality Tests Example
  • Kết thúc
  • Đọc liên quan
  • Bạn có thể sử dụng

Python OrderedDict Examples

Creating OrderedDict object

Creating OrderedDict object

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]

Output:

OrderedDict[]
OrderedDict[[['kiwi', 4], ['apple', 5], ['cat', 3]]]

Adding, Replacing, Removing items from OrderedDict

# adding elements to dict
ordered_dict['dog'] = 3

# replacing a dict key value
ordered_dict['kiwi'] = 10
print[ordered_dict]

# removing and adding a value
ordered_dict.pop['kiwi']
print[ordered_dict]
ordered_dict['kiwi'] = 4
print[ordered_dict]

Output:

OrderedDict[[['kiwi', 10], ['apple', 5], ['cat', 3], ['dog', 3]]]
OrderedDict[[['apple', 5], ['cat', 3], ['dog', 3]]]
OrderedDict[[['apple', 5], ['cat', 3], ['dog', 3], ['kiwi', 4]]]

OrderedDict move_to_end example

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
0

Output:

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
1

OrderedDict popitem example

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
2

Output:

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
3

OrderedDict Reverse Iteration

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
4

Output:

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
0

OrderedDict Equality Tests Example

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
1

Output:

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
2

Kết thúc

Đọc liên quan

Bạn có thể sử dụng

Bạn có thể sử dụng

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
3

Di chuyển một khóa hiện có đến một trong hai đầu của một từ điển được đặt hàng. Mục được di chuyển sang đầu bên phải nếu cuối cùng là đúng [mặc định] hoặc đến đầu nếu cuối cùng là sai. Tăng KeyError nếu khóa không tồn tại

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
4

Ví dụ [di chuyển

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
8 để bắt đầu từ điển để đạt được đầu ra của bạn]:
>>> d = OrderedDict[]
>>> for c in 'ABCDE':
...     d[c.lower[]] = c
...
>>> d
OrderedDict[[['a', 'A'], ['b', 'B'], ['c', 'C'], ['d', 'D'], ['e', 'E']]]
>>> d.move_to_end['e',last=False] # last=False moves to beginning
>>> d
OrderedDict[[['e', 'E'], ['a', 'A'], ['b', 'B'], ['c', 'C'], ['d', 'D']]]

Python OrderedDict is a dict subclass that maintains the items insertion order. When we iterate over an OrderedDict, items are returned in the order they were inserted. A regular dictionary doesn’t track the insertion order. So when iterating over it, items are returned in an arbitrary order. When we want to make sure that items are returned in the order they were inserted, we can use OrderedDict.

Nội dung chínhsắp xếp của Python . Hàm được sắp xếp có trong các mục của từ điển, đó là danh sách các bộ dữ liệu đại diện cho các cặp khóa của từ điển. Nó sắp xếp chúng và sau đó chuyển chúng vào OrderedDict, thứ sẽ giữ trật tự của chúng. Vì vậy, khi chúng ta đi in ra các khóa và giá trị, chúng theo thứ tự chúng ta mong đợi. Nếu bạn lặp qua một từ điển thông thường [không phải là danh sách các khóa được sắp xếp], thứ tự sẽ thay đổi mọi lúc.

OrderedDict popitem exampleFIFO order. It accepts a boolean argument

OrderedDict[]
OrderedDict[[['kiwi', 4], ['apple', 5], ['cat', 3]]]
1, if it’s set to
OrderedDict[]
OrderedDict[[['kiwi', 4], ['apple', 5], ['cat', 3]]]
2 then items are returned in LIFO order.

OrderedDict is part of python collections module.

We can create an empty

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
9 and add items to it. If we create an OrderedDict by passing a dict argument, then the ordering may be lost because dict doesn’t maintain the insertion order.popitemmove_to_end . Phương thức popitem sẽ trả về và xóa một cặp [khóa, vật phẩm]. Phương thức move_to_end sẽ di chuyển một khóa hiện có đến một trong hai đầu của OrderedDict. Mục này sẽ được chuyển sang phải đến cuối nếu đối số cuối cùng cho OrderedDict được đặt thành True [là mặc định] hoặc bắt đầu nếu nó là Sai.

Thật thú vị, OrderedDicts hỗ trợ phép lặp ngược bằng cách sử dụng hàm tích hợp đảo ngược của Python:

from collections import OrderedDict

# creating a simple dict
my_dict = {'kiwi': 4, 'apple': 5, 'cat': 3}

# creating empty ordered dict
ordered_dict = OrderedDict[]
print[ordered_dict]

# creating ordered dict from dict
ordered_dict = OrderedDict[my_dict]
print[ordered_dict]
6

Khá gọn gàng, mặc dù bạn có thể sẽ không cần chức năng đó mỗi ngày.

Kết thúc

Tại thời điểm này, bạn nên sẵn sàng dùng thử OrderedDict cho chính mình. Đó là một bổ sung hữu ích cho bộ công cụ của bạn mà tôi hy vọng bạn sẽ tìm thấy nhiều cách sử dụng trong cơ sở mã của mình.

Đọc liên quan

  • Tài liệu chính thức   cho OrderedDict
  • Mô-đun Python trong tuần:  OrderedDict

4 hữu ích 0 bình luận 9.7k xem chia sẻ 0 bình luận 9.7k xem chia sẻ 0 bình luận 9.7k xem chia sẻ

Chủ Đề