Lệnh python dict mặc định có được đặt hàng không?

Mô-đun này triển khai các kiểu dữ liệu vùng chứa chuyên dụng cung cấp các lựa chọn thay thế cho các vùng chứa tích hợp sẵn cho mục đích chung của Python,

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
7,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
8 và
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
9

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
0

chức năng xuất xưởng để tạo các lớp con tuple với các trường được đặt tên

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
1

vùng chứa giống như danh sách với các phần bổ sung và bật lên nhanh chóng ở hai đầu

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
2

lớp giống như dict để tạo một chế độ xem nhiều ánh xạ

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
3

lớp con dict để đếm các đối tượng có thể băm

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4

lớp con dict ghi nhớ các mục nhập đơn hàng đã được thêm vào

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
5

lớp con dict gọi hàm xuất xưởng để cung cấp các giá trị còn thiếu

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
6

trình bao bọc xung quanh các đối tượng từ điển để phân lớp dict dễ dàng hơn

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
7

trình bao bọc xung quanh các đối tượng danh sách để phân lớp danh sách dễ dàng hơn

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
8

trình bao bọc xung quanh các đối tượng chuỗi để phân lớp chuỗi dễ dàng hơn

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
2 đối tượng¶

Mới trong phiên bản 3. 3

Một lớp

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
2 được cung cấp để nhanh chóng liên kết một số ánh xạ để chúng có thể được coi là một đơn vị duy nhất. Nó thường nhanh hơn nhiều so với việc tạo một từ điển mới và chạy nhiều lệnh gọi
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
51

Lớp này có thể được sử dụng để mô phỏng các phạm vi lồng nhau và rất hữu ích trong việc tạo khuôn mẫu

lớp bộ sưu tập. Bản đồ chuỗi[*bản đồ]

Một

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
2 nhóm nhiều ký tự hoặc ánh xạ khác lại với nhau để tạo một chế độ xem duy nhất, có thể cập nhật. Nếu không có bản đồ nào được chỉ định, một từ điển trống duy nhất sẽ được cung cấp để một chuỗi mới luôn có ít nhất một bản đồ

Các ánh xạ cơ bản được lưu trữ trong một danh sách. Danh sách đó là công khai và có thể được truy cập hoặc cập nhật bằng thuộc tính bản đồ. Không có trạng thái nào khác

Tra cứu liên tục tìm kiếm các ánh xạ cơ bản cho đến khi tìm thấy khóa. Ngược lại, ghi, cập nhật và xóa chỉ hoạt động trên ánh xạ đầu tiên

Một

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
2 kết hợp các ánh xạ cơ bản bằng tham chiếu. Vì vậy, nếu một trong các ánh xạ cơ bản được cập nhật, những thay đổi đó sẽ được phản ánh trong
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
2

Tất cả các phương pháp từ điển thông thường đều được hỗ trợ. Ngoài ra, còn có thuộc tính bản đồ, phương thức tạo ngữ cảnh con mới và thuộc tính để truy cập tất cả trừ ánh xạ đầu tiên

bản đồ

Danh sách ánh xạ có thể cập nhật của người dùng. Danh sách được sắp xếp từ tìm kiếm đầu tiên đến tìm kiếm cuối cùng. Đây là trạng thái được lưu trữ duy nhất và có thể được sửa đổi để thay đổi ánh xạ nào được tìm kiếm. Danh sách phải luôn chứa ít nhất một ánh xạ

new_child[m=Không có, **kwargs]

Trả về một

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
2 mới chứa một bản đồ mới, theo sau là tất cả các bản đồ trong phiên bản hiện tại. Nếu
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
56 được chỉ định, nó sẽ trở thành bản đồ mới ở đầu danh sách các ánh xạ; .
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
58. Nếu bất kỳ đối số từ khóa nào được chỉ định, chúng sẽ cập nhật bản đồ đã qua hoặc lệnh trống mới. Phương pháp này được sử dụng để tạo các ngữ cảnh con có thể được cập nhật mà không làm thay đổi giá trị trong bất kỳ ánh xạ gốc nào

Đã thay đổi trong phiên bản 3. 4. Tham số

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
56 tùy chọn đã được thêm vào.

Đã thay đổi trong phiên bản 3. 10. Hỗ trợ đối số từ khóa đã được thêm vào.

cha mẹ

Thuộc tính trả về một

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
2 mới chứa tất cả các bản đồ trong phiên bản hiện tại ngoại trừ bản đồ đầu tiên. Điều này hữu ích cho việc bỏ qua bản đồ đầu tiên trong tìm kiếm. Các trường hợp sử dụng tương tự như các trường hợp cho từ khóa
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
51 được sử dụng trong phạm vi lồng nhau . Các trường hợp sử dụng cũng song song với các trường hợp sử dụng chức năng
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
52 tích hợp. Một tham chiếu đến
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
53 tương đương với.
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
54.

Lưu ý, thứ tự lặp lại của một

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
55 được xác định bằng cách quét các ánh xạ từ cuối đến trước

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
0

Điều này đưa ra thứ tự giống như một loạt các cuộc gọi

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
56 bắt đầu với ánh xạ cuối cùng

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
2

Đã thay đổi trong phiên bản 3. 9. Đã thêm hỗ trợ cho toán tử

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
57 và
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
58, được chỉ định trong PEP 584.

Xem thêm

  • Lớp MultiContext trong gói Enthought CodeTools có các tùy chọn để hỗ trợ ghi vào bất kỳ ánh xạ nào trong chuỗi

  • Lớp Ngữ cảnh của Django để tạo khuôn mẫu là một chuỗi ánh xạ chỉ đọc. Nó cũng có tính năng đẩy và bật ngữ cảnh tương tự như phương thức

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    59 và thuộc tính
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    60

  • Công thức Ngữ cảnh lồng nhau có các tùy chọn để kiểm soát việc ghi và các đột biến khác chỉ áp dụng cho ánh xạ đầu tiên hay bất kỳ ánh xạ nào trong chuỗi

  • Phiên bản chỉ đọc được đơn giản hóa rất nhiều của Chainmap

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
2 Ví dụ và Công thức¶

Phần này cho thấy các cách tiếp cận khác nhau để làm việc với bản đồ chuỗi

Ví dụ mô phỏng chuỗi tra cứu nội bộ của Python

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
8

Ví dụ về việc cho phép các đối số dòng lệnh do người dùng chỉ định được ưu tiên hơn các biến môi trường, các biến này sẽ được ưu tiên hơn các giá trị mặc định

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
9

Các mẫu ví dụ để sử dụng lớp

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
2 để mô phỏng các bối cảnh lồng nhau

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary

Lớp

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
2 chỉ thực hiện cập nhật [ghi và xóa] đối với ánh xạ đầu tiên trong chuỗi trong khi tra cứu sẽ tìm kiếm toàn bộ chuỗi. Tuy nhiên, nếu muốn ghi và xóa sâu, có thể dễ dàng tạo một lớp con cập nhật các khóa được tìm thấy sâu hơn trong chuỗi

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
3 đối tượng¶

Một công cụ truy cập được cung cấp để hỗ trợ kiểm đếm thuận tiện và nhanh chóng. Ví dụ

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
5

lớp bộ sưu tập. Bộ đếm[[lặp lại hoặc ánh xạ]]

Một

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
3 là một phân lớp
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6 để đếm các đối tượng có thể băm. Nó là một bộ sưu tập nơi các phần tử được lưu trữ dưới dạng khóa từ điển và số lượng của chúng được lưu trữ dưới dạng giá trị từ điển. Số lượng được phép là bất kỳ giá trị số nguyên nào kể cả số không hoặc số âm. Lớp
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
3 tương tự như túi hoặc nhiều bộ trong các ngôn ngữ khác

Các phần tử được tính từ một lần lặp hoặc được khởi tạo từ một ánh xạ [hoặc bộ đếm] khác

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
5

Các đối tượng bộ đếm có giao diện từ điển ngoại trừ việc chúng trả về số 0 cho các mục bị thiếu thay vì tăng

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
68

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6

Đặt số đếm thành 0 không xóa phần tử khỏi bộ đếm. Sử dụng

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
69 để xóa nó hoàn toàn

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
3

Mới trong phiên bản 3. 1

Đã thay đổi trong phiên bản 3. 7. Là một lớp con của

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6,
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
3 thừa hưởng khả năng ghi nhớ thứ tự chèn. Các phép toán trên các đối tượng Counter cũng bảo toàn thứ tự. Các kết quả được sắp xếp theo thời điểm một phần tử được tìm thấy đầu tiên trong toán hạng bên trái và sau đó theo thứ tự gặp phải trong toán hạng bên phải.

Các đối tượng truy cập hỗ trợ các phương thức bổ sung ngoài những phương thức có sẵn cho tất cả các từ điển

phần tử[]

Trả về một trình vòng lặp qua các phần tử lặp lại nhiều lần bằng số lượng của nó. Các phần tử được trả về theo thứ tự gặp phải lần đầu tiên. Nếu số lượng của một phần tử nhỏ hơn một, thì

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
32 sẽ bỏ qua nó

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
20

most_common[[n]]

Trả về danh sách n phần tử phổ biến nhất và số lượng của chúng từ phổ biến nhất đến ít nhất. Nếu n bị bỏ qua hoặc

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
33,
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
34 trả về tất cả các phần tử trong bộ đếm. Các phần tử có số lượng bằng nhau được sắp xếp theo thứ tự gặp phải lần đầu tiên

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
21

trừ[[iterable-or-mapping]]

Các phần tử được trừ khỏi một lần lặp hoặc từ một ánh xạ [hoặc bộ đếm] khác. Giống như

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
56 nhưng trừ đi số lượng thay vì thay thế chúng. Cả đầu vào và đầu ra có thể bằng 0 hoặc âm

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
22

Mới trong phiên bản 3. 2

tổng[]

Tính tổng các số

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
23

Mới trong phiên bản 3. 10

Các phương thức từ điển thông thường có sẵn cho các đối tượng

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
3 ngoại trừ hai đối tượng hoạt động khác nhau đối với bộ đếm

từ khóa[có thể lặp lại]

Phương thức lớp này không được triển khai cho các đối tượng

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
3

cập nhật[[lặp lại hoặc ánh xạ]]

Các phần tử được tính từ một lần lặp hoặc phần bổ sung từ một ánh xạ [hoặc bộ đếm] khác. Giống như

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
56 nhưng thêm số lượng thay vì thay thế chúng. Ngoài ra, iterable dự kiến ​​là một chuỗi các phần tử, không phải là chuỗi các cặp
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
39

Bộ đếm hỗ trợ các toán tử so sánh phong phú cho các mối quan hệ đẳng thức, tập hợp con và siêu tập hợp.

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
200,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
201, ________ 3202, ________ 3203, ________ 3204, ________ 3205. Tất cả các thử nghiệm đó đều coi các phần tử bị thiếu là không có số lượng để
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
206 trả về giá trị true

Mới trong phiên bản 3. 10. Các thao tác so sánh phong phú đã được thêm vào.

Đã thay đổi trong phiên bản 3. 10. Trong các bài kiểm tra bằng, các phần tử bị thiếu được coi là không có số đếm. Trước đây,

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
207 và
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
208 được coi là khác biệt.

Các mẫu phổ biến để làm việc với các đối tượng

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
3

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
24

Một số phép toán được cung cấp để kết hợp các đối tượng

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
3 để tạo ra nhiều bộ [bộ đếm có số đếm lớn hơn 0]. Phép cộng và phép trừ kết hợp các bộ đếm bằng cách cộng hoặc trừ số lượng các phần tử tương ứng. Giao lộ và liên kết trả lại số lượng tối thiểu và tối đa tương ứng. Bình đẳng và bao gồm so sánh số lượng tương ứng. Mỗi hoạt động có thể chấp nhận đầu vào có số lượng đã ký, nhưng đầu ra sẽ loại trừ các kết quả có số lượng bằng 0 hoặc ít hơn

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
25

Phép cộng và phép trừ đơn vị là các phím tắt để cộng một bộ đếm trống hoặc trừ một bộ đếm trống

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
26

Mới trong phiên bản 3. 3. Đã thêm hỗ trợ cho các hoạt động cộng một ngôi, trừ một ngôi và nhiều tập hợp tại chỗ.

Ghi chú

Bộ đếm chủ yếu được thiết kế để hoạt động với các số nguyên dương để biểu thị số lượng đang chạy; . Để trợ giúp với các trường hợp sử dụng đó, phần này ghi lại các hạn chế về phạm vi và loại tối thiểu

  • Bản thân lớp

    class DeepChainMap[ChainMap]:
        'Variant of ChainMap that allows direct updates to inner scopes'
    
        def __setitem__[self, key, value]:
            for mapping in self.maps:
                if key in mapping:
                    mapping[key] = value
                    return
            self.maps[0][key] = value
    
        def __delitem__[self, key]:
            for mapping in self.maps:
                if key in mapping:
                    del mapping[key]
                    return
            raise KeyError[key]
    
    >>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
    >>> d['lion'] = 'orange'         # update an existing key two levels down
    >>> d['snake'] = 'red'           # new keys get added to the topmost dict
    >>> del d['elephant']            # remove an existing key one level down
    >>> d                            # display result
    DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
    
    3 là một lớp con từ điển không có giới hạn về khóa và giá trị của nó. Các giá trị được dự định là các số đại diện cho số lượng, nhưng bạn có thể lưu trữ bất kỳ thứ gì trong trường giá trị

  • Phương pháp

    class DeepChainMap[ChainMap]:
        'Variant of ChainMap that allows direct updates to inner scopes'
    
        def __setitem__[self, key, value]:
            for mapping in self.maps:
                if key in mapping:
                    mapping[key] = value
                    return
            self.maps[0][key] = value
    
        def __delitem__[self, key]:
            for mapping in self.maps:
                if key in mapping:
                    del mapping[key]
                    return
            raise KeyError[key]
    
    >>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
    >>> d['lion'] = 'orange'         # update an existing key two levels down
    >>> d['snake'] = 'red'           # new keys get added to the topmost dict
    >>> del d['elephant']            # remove an existing key one level down
    >>> d                            # display result
    DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
    
    34 chỉ yêu cầu các giá trị có thể sắp xếp được

  • Đối với các hoạt động tại chỗ, chẳng hạn như

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    213, loại giá trị chỉ cần hỗ trợ phép cộng và phép trừ. Vì vậy, phân số, số thực và số thập phân sẽ hoạt động và các giá trị âm được hỗ trợ. Điều này cũng đúng với
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    51 và
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    215 cho phép các giá trị âm và 0 cho cả đầu vào và đầu ra

  • Các phương thức multiset chỉ được thiết kế cho các trường hợp sử dụng có giá trị dương. Các đầu vào có thể âm hoặc bằng 0, nhưng chỉ các đầu ra có giá trị dương mới được tạo. Không có hạn chế về loại, nhưng loại giá trị cần hỗ trợ cộng, trừ và so sánh

  • Phương pháp

    class DeepChainMap[ChainMap]:
        'Variant of ChainMap that allows direct updates to inner scopes'
    
        def __setitem__[self, key, value]:
            for mapping in self.maps:
                if key in mapping:
                    mapping[key] = value
                    return
            self.maps[0][key] = value
    
        def __delitem__[self, key]:
            for mapping in self.maps:
                if key in mapping:
                    del mapping[key]
                    return
            raise KeyError[key]
    
    >>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
    >>> d['lion'] = 'orange'         # update an existing key two levels down
    >>> d['snake'] = 'red'           # new keys get added to the topmost dict
    >>> del d['elephant']            # remove an existing key one level down
    >>> d                            # display result
    DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
    
    32 yêu cầu số nguyên. Nó bỏ qua số không và số âm

Xem thêm

  • Lớp túi trong Smalltalk

  • Mục nhập Wikipedia cho Multisets

  • Hướng dẫn multiset C++ với các ví dụ

  • Đối với các hoạt động toán học trên multisets và các trường hợp sử dụng của chúng, xem Knuth, Donald. Nghệ thuật lập trình máy tính Tập II, Phần 4. 6. 3, Bài tập 19

  • Để liệt kê tất cả các tập hợp nhiều phần riêng biệt có kích thước nhất định trên một tập hợp các phần tử đã cho, hãy xem

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    217

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    27

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
1 đối tượng¶

lớp bộ sưu tập. deque[[có thể lặp lại[, maxlen]]]

Trả về một đối tượng deque mới được khởi tạo từ trái sang phải [sử dụng

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
219] với dữ liệu từ iterable. Nếu iterable không được chỉ định, deque mới trống

Deques là sự khái quát hóa của ngăn xếp và hàng đợi [tên được phát âm là “bộ bài” và là viết tắt của “hàng đợi hai đầu”]. Deques hỗ trợ nối thêm và bật hiệu quả bộ nhớ, an toàn cho luồng từ hai bên của deque với hiệu suất O[1] xấp xỉ như nhau theo cả hai hướng

Mặc dù các đối tượng

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
7 hỗ trợ các thao tác tương tự, nhưng chúng được tối ưu hóa cho các thao tác có độ dài cố định nhanh và phát sinh chi phí di chuyển bộ nhớ O[n] cho các thao tác
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
221 và
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
222 làm thay đổi cả kích thước và vị trí của biểu diễn dữ liệu cơ bản

Nếu maxlen không được chỉ định hoặc là

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
33, deques có thể phát triển đến độ dài tùy ý. Mặt khác, deque được giới hạn ở độ dài tối đa được chỉ định. Khi deque có độ dài giới hạn đầy, khi các mục mới được thêm vào, một số mục tương ứng sẽ bị loại bỏ ở đầu đối diện. Bounded length deques cung cấp chức năng tương tự như bộ lọc
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
224 trong Unix. Chúng cũng hữu ích để theo dõi các giao dịch và các nhóm dữ liệu khác chỉ quan tâm đến hoạt động gần đây nhất

Các đối tượng Deque hỗ trợ các phương thức sau

chắp thêm[x]

Thêm x vào bên phải của deque

appendleft[x]

Thêm x vào bên trái của deque

xóa[]

Xóa tất cả các phần tử khỏi deque để lại nó có độ dài 0

bản sao[]

Tạo một bản sao nông của deque

Mới trong phiên bản 3. 5

đếm[x]

Đếm số phần tử deque bằng x

Mới trong phiên bản 3. 2

mở rộng[có thể lặp lại]

Mở rộng phía bên phải của deque bằng cách nối thêm các phần tử từ đối số có thể lặp lại

extendleft[có thể lặp lại]

Mở rộng phía bên trái của deque bằng cách nối thêm các phần tử từ iterable. Lưu ý, chuỗi nối trái dẫn đến đảo ngược thứ tự các phần tử trong đối số có thể lặp lại

index[x[ , start[, stop]]]

Trả về vị trí của x trong deque [tại hoặc sau khi bắt đầu chỉ mục và trước khi dừng chỉ mục]. Trả về trận đấu đầu tiên hoặc tăng

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
225 nếu không tìm thấy

Mới trong phiên bản 3. 5

insert[i , x]

Chèn x vào deque tại vị trí i

Nếu việc chèn sẽ khiến deque có giới hạn phát triển vượt quá giá trị tối đa, thì một

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
226 sẽ được nâng lên

Mới trong phiên bản 3. 5

pop[]

Xóa và trả lại một phần tử từ phía bên phải của deque. Nếu không có phần tử nào, hãy tăng một

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
226

popleft[]

Xóa và trả lại một phần tử từ phía bên trái của deque. Nếu không có phần tử nào, hãy tăng một

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
226

xóa[giá trị]

Xóa lần xuất hiện đầu tiên của giá trị. Nếu không tìm thấy, tăng một

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
225

đảo ngược[]

Đảo ngược các phần tử của deque tại chỗ và sau đó trả về

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
33

Mới trong phiên bản 3. 2

xoay[n=1]

Xoay deque n bước sang phải. Nếu n âm, xoay sang trái

Khi deque không trống, xoay một bước sang phải tương đương với

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
231 và xoay một bước sang trái tương đương với
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
232

Các đối tượng Deque cũng cung cấp một thuộc tính chỉ đọc

maxlen

Kích thước tối đa của deque hoặc

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
33 nếu không bị chặn

Mới trong phiên bản 3. 1

Ngoài những điều trên, deques hỗ trợ lặp, pickling,

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
234,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
235,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
236,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
237, kiểm tra tư cách thành viên với toán tử
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
238 và tham chiếu chỉ số dưới như
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
239 để truy cập phần tử đầu tiên. Truy cập được lập chỉ mục là O[1] ở cả hai đầu nhưng chậm lại thành O[n] ở giữa. Để truy cập ngẫu nhiên nhanh, hãy sử dụng danh sách thay thế

Bắt đầu từ phiên bản 3. 5, deques hỗ trợ

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
240,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
241, và
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
242

Thí dụ

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
28

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
1 Công thức¶

Phần này cho thấy các cách tiếp cận khác nhau để làm việc với deques

Bounded length deques cung cấp chức năng tương tự như bộ lọc

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
224 trong Unix

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
29

Một cách tiếp cận khác để sử dụng deques là duy trì một chuỗi các phần tử được thêm gần đây bằng cách thêm vào bên phải và bật ra bên trái

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
80

Có thể triển khai bộ lập lịch quay vòng với các bộ lặp đầu vào được lưu trữ trong một

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
1. Các giá trị được tạo ra từ trình vòng lặp đang hoạt động ở vị trí 0. Nếu trình vòng lặp đó đã hết, nó có thể bị xóa bằng
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
246;

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
81

Phương thức

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
247 cung cấp một cách để thực hiện cắt và xóa
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
1. Ví dụ: một triển khai Python thuần túy của
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
250 dựa vào phương thức
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
247 để định vị các phần tử sẽ được bật lên

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
82

Để thực hiện cắt lát

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
1, hãy sử dụng cách tiếp cận tương tự áp dụng
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
247 để đưa phần tử đích sang phía bên trái của deque. Xóa các mục cũ bằng
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
246, thêm các mục mới bằng
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
255, sau đó đảo ngược vòng quay. Với các biến thể nhỏ trong cách tiếp cận đó, thật dễ dàng thực hiện các thao tác ngăn xếp kiểu Forth, chẳng hạn như
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
256,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
257,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
258,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
259,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
260,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
261 và
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
262

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
5 đối tượng¶

lớp bộ sưu tập. defaultdict[default_factory=None , /[, ...]]

Trả về một đối tượng giống như từ điển mới.

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
5 là một lớp con của lớp
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6 tích hợp. Nó ghi đè một phương thức và thêm một biến thể hiện có thể ghi. Các chức năng còn lại giống như đối với lớp
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6 và không được ghi lại ở đây

Đối số đầu tiên cung cấp giá trị ban đầu cho thuộc tính

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
267; . Tất cả các đối số còn lại được xử lý giống như khi chúng được chuyển đến hàm tạo
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6, bao gồm cả các đối số từ khóa

Các đối tượng

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
5 hỗ trợ phương thức sau ngoài các hoạt động tiêu chuẩn của
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6

__missing__[phím]

Nếu thuộc tính

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
267 là
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
33, điều này sẽ tạo ra một ngoại lệ
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
68 với khóa là đối số

Nếu

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
267 không phải là
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
33, thì nó được gọi mà không có đối số để cung cấp giá trị mặc định cho khóa đã cho, giá trị này được chèn vào từ điển cho khóa và được trả về

Nếu gọi

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
267 đưa ra một ngoại lệ thì ngoại lệ này sẽ được truyền bá không thay đổi

Phương thức này được gọi bởi phương thức

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
278 của lớp
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6 khi không tìm thấy khóa được yêu cầu;

Lưu ý rằng

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
281 không được gọi cho bất kỳ hoạt động nào ngoài
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
278. Điều này có nghĩa là
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
283, giống như các từ điển thông thường, sẽ trả về giá trị mặc định là
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
33 thay vì sử dụng
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
267

Các đối tượng

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
5 hỗ trợ biến đối tượng sau

default_factory

Thuộc tính này được sử dụng bởi phương pháp

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
281;

Đã thay đổi trong phiên bản 3. 9. Đã thêm toán tử hợp nhất [

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
57] và cập nhật [
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
58], được chỉ định trong PEP 584.

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
5 Ví dụ¶

Sử dụng

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
7 làm
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
267, thật dễ dàng để nhóm một chuỗi các cặp khóa-giá trị thành một danh sách từ điển

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
83

Khi mỗi khóa được tìm thấy lần đầu tiên, nó chưa có trong ánh xạ; . Thao tác

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
296 sau đó gắn giá trị vào danh sách mới. Khi gặp lại các phím, quá trình tra cứu sẽ diễn ra bình thường [trả về danh sách cho phím đó] và thao tác
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
296 sẽ thêm một giá trị khác vào danh sách. Kỹ thuật này đơn giản và nhanh hơn so với kỹ thuật tương đương sử dụng
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
298

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
84

Đặt

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
267 thành
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
800 làm cho
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
5 trở nên hữu ích khi đếm [như một cái túi hoặc nhiều bộ trong các ngôn ngữ khác]

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
85

Khi lần đầu tiên gặp một chữ cái, nó bị thiếu trong ánh xạ, vì vậy hàm

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
267 gọi
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
803 để cung cấp số lượng mặc định là 0. Sau đó, thao tác tăng dần sẽ tạo số lượng cho mỗi chữ cái

Hàm

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
803 luôn trả về 0 chỉ là trường hợp đặc biệt của hàm hằng. Một cách nhanh hơn và linh hoạt hơn để tạo các hàm hằng số là sử dụng hàm lambda có thể cung cấp bất kỳ giá trị hằng số nào [không chỉ bằng 0]

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
86

Đặt

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
267 thành
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
8 làm cho
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
5 trở nên hữu ích để xây dựng từ điển các tập hợp

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
87

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
0 Hàm xuất xưởng cho các bộ có trường được đặt tên¶

Các bộ dữ liệu được đặt tên gán ý nghĩa cho từng vị trí trong một bộ dữ liệu và cho phép mã tự ghi lại, dễ đọc hơn. Chúng có thể được sử dụng ở bất cứ nơi nào sử dụng bộ dữ liệu thông thường và chúng thêm khả năng truy cập các trường theo tên thay vì chỉ mục vị trí

bộ sưu tập. namedtuple[typename , field_names, *, rename=False, defaults=None, module=None]

Trả về một lớp con tuple mới có tên typename. Lớp con mới được sử dụng để tạo các đối tượng giống như bộ dữ liệu có các trường có thể truy cập được bằng cách tra cứu thuộc tính cũng như có thể lập chỉ mục và có thể lặp lại. Các thể hiện của lớp con cũng có một chuỗi tài liệu hữu ích [với tên kiểu và tên_trường] và một phương thức

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
809 hữu ích liệt kê các nội dung của bộ theo định dạng
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
810

Tên trường là một chuỗi các chuỗi chẳng hạn như

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
811. Ngoài ra, field_names có thể là một chuỗi với mỗi tên trường được phân tách bằng khoảng trắng và/hoặc dấu phẩy, ví dụ:
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
812 hoặc
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
813

Bất kỳ mã định danh Python hợp lệ nào cũng có thể được sử dụng cho tên trường ngoại trừ tên bắt đầu bằng dấu gạch dưới. Số nhận dạng hợp lệ bao gồm các chữ cái, chữ số và dấu gạch dưới nhưng không bắt đầu bằng chữ số hoặc dấu gạch dưới và không thể là một

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
814 chẳng hạn như lớp, cho, trả lại, toàn cầu, vượt qua hoặc nâng cao

Nếu đổi tên là đúng, tên trường không hợp lệ sẽ tự động được thay thế bằng tên vị trí. Ví dụ:

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
815 được chuyển đổi thành
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
816, loại bỏ từ khóa
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
817 và tên trường trùng lặp
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
818

giá trị mặc định có thể là

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
33 hoặc có thể lặp lại giá trị mặc định. Vì các trường có giá trị mặc định phải xuất hiện sau bất kỳ trường nào không có giá trị mặc định, nên giá trị mặc định được áp dụng cho các tham số ngoài cùng bên phải. Ví dụ: nếu tên trường là
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
820 và giá trị mặc định là
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
821, thì
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
822 sẽ là đối số bắt buộc,
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
823 sẽ mặc định là
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
824 và
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
825 sẽ mặc định là
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
826.

Nếu mô-đun được xác định, thuộc tính

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
827 của bộ dữ liệu được đặt tên được đặt thành giá trị đó

Các phiên bản bộ dữ liệu được đặt tên không có từ điển cho mỗi phiên bản, vì vậy chúng nhẹ và không yêu cầu nhiều bộ nhớ hơn các bộ dữ liệu thông thường

Để hỗ trợ tẩy, lớp bộ dữ liệu được đặt tên phải được gán cho một biến khớp với tên loại

Đã thay đổi trong phiên bản 3. 1. Đã thêm hỗ trợ đổi tên.

Đã thay đổi trong phiên bản 3. 6. Thông số dài dòng và đổi tên trở thành đối số chỉ từ khóa .

Đã thay đổi trong phiên bản 3. 6. Đã thêm tham số mô-đun.

Đã thay đổi trong phiên bản 3. 7. Đã xóa tham số dài dòng và thuộc tính

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
828.

Đã thay đổi trong phiên bản 3. 7. Đã thêm tham số mặc định và thuộc tính

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
829.

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
88

Các bộ dữ liệu được đặt tên đặc biệt hữu ích để gán tên trường cho các bộ dữ liệu kết quả được trả về bởi các mô-đun

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
830 hoặc
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
831

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
89

Ngoài các phương thức kế thừa từ các bộ dữ liệu, các bộ dữ liệu được đặt tên hỗ trợ ba phương thức bổ sung và hai thuộc tính. Để tránh xung đột với tên trường, tên phương thức và thuộc tính bắt đầu bằng dấu gạch dưới

phương thức lớp somenamedtuple. _make[có thể lặp lại]

Phương thức lớp tạo một thể hiện mới từ một chuỗi hiện có hoặc có thể lặp lại

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
90

somenamedtuple. _asdict[]

Trả lại một

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6 mới để ánh xạ tên trường tới các giá trị tương ứng của chúng

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
91

Đã thay đổi trong phiên bản 3. 1. Trả về một

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4 thay vì một
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6 thông thường.

Đã thay đổi trong phiên bản 3. 8. Trả về một

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6 thông thường thay vì một
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4. Kể từ Python 3. 7, lệnh thường xuyên được đảm bảo để được đặt hàng. Nếu các tính năng bổ sung của
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4 được yêu cầu, thì biện pháp khắc phục được đề xuất là chuyển kết quả sang loại mong muốn.
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
838.

somenamedtuple. _replace[**kwargs]

Trả về một thể hiện mới của bộ dữ liệu được đặt tên thay thế các trường đã chỉ định bằng các giá trị mới

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
92

somenamedtuple. _trường

Bộ chuỗi liệt kê tên trường. Hữu ích cho việc xem xét nội quan và để tạo các loại bộ dữ liệu được đặt tên mới từ các bộ dữ liệu được đặt tên hiện có

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
93

somenamedtuple. _field_defaults

Tên trường ánh xạ từ điển thành giá trị mặc định

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
94

Để truy xuất một trường có tên được lưu trữ trong một chuỗi, hãy sử dụng hàm

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
839

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
95

Để chuyển đổi từ điển thành bộ có tên, hãy sử dụng toán tử sao đôi [như được mô tả trong Giải nén danh sách đối số ].

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
96

Vì một bộ có tên là một lớp Python thông thường, nên dễ dàng thêm hoặc thay đổi chức năng với một lớp con. Đây là cách thêm trường được tính toán và định dạng in có chiều rộng cố định

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
97

Lớp con được hiển thị ở trên đặt

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
840 thành một bộ trống. Điều này giúp giữ cho yêu cầu bộ nhớ ở mức thấp bằng cách ngăn việc tạo từ điển cá thể

Phân lớp không hữu ích để thêm các trường mới, được lưu trữ. Thay vào đó, chỉ cần tạo một loại bộ dữ liệu có tên mới từ thuộc tính

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
841

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
98

Các chuỗi tài liệu có thể được tùy chỉnh bằng cách gán trực tiếp cho các trường

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
842

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
99

Đã thay đổi trong phiên bản 3. 5. Chuỗi tài liệu thuộc tính có thể ghi được.

Xem thêm

  • Xem

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    843 để biết cách thêm gợi ý loại cho các bộ dữ liệu được đặt tên. Nó cũng cung cấp một ký hiệu thanh lịch bằng cách sử dụng từ khóa
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    844

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    0

  • Xem

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    845 để biết không gian tên có thể thay đổi dựa trên từ điển cơ bản thay vì bộ dữ liệu

  • Mô-đun

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    846 cung cấp một trình trang trí và các chức năng để tự động thêm các phương thức đặc biệt đã tạo vào các lớp do người dùng định nghĩa

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4 đối tượng¶

Từ điển có thứ tự giống như từ điển thông thường nhưng có thêm một số khả năng liên quan đến hoạt động đặt hàng. Bây giờ chúng trở nên ít quan trọng hơn khi lớp

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6 tích hợp có khả năng ghi nhớ thứ tự chèn [hành vi mới này đã được đảm bảo trong Python 3. 7]

Một số khác biệt từ

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6 vẫn còn

  • c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    6 thông thường được thiết kế rất tốt trong các hoạt động lập bản đồ. Theo dõi thứ tự chèn là thứ yếu

  • class DeepChainMap[ChainMap]:
        'Variant of ChainMap that allows direct updates to inner scopes'
    
        def __setitem__[self, key, value]:
            for mapping in self.maps:
                if key in mapping:
                    mapping[key] = value
                    return
            self.maps[0][key] = value
    
        def __delitem__[self, key]:
            for mapping in self.maps:
                if key in mapping:
                    del mapping[key]
                    return
            raise KeyError[key]
    
    >>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
    >>> d['lion'] = 'orange'         # update an existing key two levels down
    >>> d['snake'] = 'red'           # new keys get added to the topmost dict
    >>> del d['elephant']            # remove an existing key one level down
    >>> d                            # display result
    DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
    
    4 được thiết kế để thực hiện tốt các hoạt động sắp xếp lại. Hiệu quả về không gian, tốc độ lặp lại và hiệu suất của các hoạt động cập nhật chỉ là thứ yếu

  • Thuật toán

    class DeepChainMap[ChainMap]:
        'Variant of ChainMap that allows direct updates to inner scopes'
    
        def __setitem__[self, key, value]:
            for mapping in self.maps:
                if key in mapping:
                    mapping[key] = value
                    return
            self.maps[0][key] = value
    
        def __delitem__[self, key]:
            for mapping in self.maps:
                if key in mapping:
                    del mapping[key]
                    return
            raise KeyError[key]
    
    >>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
    >>> d['lion'] = 'orange'         # update an existing key two levels down
    >>> d['snake'] = 'red'           # new keys get added to the topmost dict
    >>> del d['elephant']            # remove an existing key one level down
    >>> d                            # display result
    DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
    
    4 có thể xử lý các hoạt động sắp xếp lại thường xuyên tốt hơn
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    6. Như thể hiện trong các công thức bên dưới, điều này làm cho nó phù hợp để triển khai các loại bộ đệm LRU khác nhau

  • Phép toán đẳng thức cho

    class DeepChainMap[ChainMap]:
        'Variant of ChainMap that allows direct updates to inner scopes'
    
        def __setitem__[self, key, value]:
            for mapping in self.maps:
                if key in mapping:
                    mapping[key] = value
                    return
            self.maps[0][key] = value
    
        def __delitem__[self, key]:
            for mapping in self.maps:
                if key in mapping:
                    del mapping[key]
                    return
            raise KeyError[key]
    
    >>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
    >>> d['lion'] = 'orange'         # update an existing key two levels down
    >>> d['snake'] = 'red'           # new keys get added to the topmost dict
    >>> del d['elephant']            # remove an existing key one level down
    >>> d                            # display result
    DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
    
    4 kiểm tra khớp lệnh

    Một

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    6 thông thường có thể mô phỏng bài kiểm tra đẳng thức theo thứ tự với
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    856

  • Phương thức

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    857 của
    class DeepChainMap[ChainMap]:
        'Variant of ChainMap that allows direct updates to inner scopes'
    
        def __setitem__[self, key, value]:
            for mapping in self.maps:
                if key in mapping:
                    mapping[key] = value
                    return
            self.maps[0][key] = value
    
        def __delitem__[self, key]:
            for mapping in self.maps:
                if key in mapping:
                    del mapping[key]
                    return
            raise KeyError[key]
    
    >>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
    >>> d['lion'] = 'orange'         # update an existing key two levels down
    >>> d['snake'] = 'red'           # new keys get added to the topmost dict
    >>> del d['elephant']            # remove an existing key one level down
    >>> d                            # display result
    DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
    
    4 có chữ ký khác. Nó chấp nhận một đối số tùy chọn để chỉ định mục nào được bật lên

    Một

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    6 thông thường có thể mô phỏng OrderedDict's
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    860 với
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    861 được đảm bảo bật mục ngoài cùng bên phải [cuối cùng]

    Một

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    6 thông thường có thể mô phỏng
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    863 của OrderedDict với
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    864 sẽ trả về và xóa mục ngoài cùng bên trái [đầu tiên] nếu nó tồn tại

  • class DeepChainMap[ChainMap]:
        'Variant of ChainMap that allows direct updates to inner scopes'
    
        def __setitem__[self, key, value]:
            for mapping in self.maps:
                if key in mapping:
                    mapping[key] = value
                    return
            self.maps[0][key] = value
    
        def __delitem__[self, key]:
            for mapping in self.maps:
                if key in mapping:
                    del mapping[key]
                    return
            raise KeyError[key]
    
    >>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
    >>> d['lion'] = 'orange'         # update an existing key two levels down
    >>> d['snake'] = 'red'           # new keys get added to the topmost dict
    >>> del d['elephant']            # remove an existing key one level down
    >>> d                            # display result
    DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
    
    4 có phương pháp
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    866 để định vị lại một phần tử thành điểm cuối một cách hiệu quả

    Một

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    6 thông thường có thể mô phỏng
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    868 của OrderedDict với
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    869 sẽ di chuyển khóa và giá trị được liên kết của nó đến vị trí ngoài cùng bên phải [cuối cùng]

    Một

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    6 thông thường không có một giá trị hiệu quả tương đương với
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    871 của OrderedDict, vốn sẽ di chuyển khóa và giá trị được liên kết của nó sang vị trí ngoài cùng bên trái [đầu tiên]

  • Cho đến Python 3. 8,

    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    6 thiếu phương pháp
    c = ChainMap[]        # Create root context
    d = c.new_child[]     # Create nested child context
    e = c.new_child[]     # Child of c, independent from d
    e.maps[0]             # Current context dictionary -- like Python's locals[]
    e.maps[-1]            # Root context -- like Python's globals[]
    e.parents             # Enclosing context chain -- like Python's nonlocals
    
    d['x'] = 1            # Set value in current context
    d['x']                # Get first key in the chain of contexts
    del d['x']            # Delete from current context
    list[d]               # All nested values
    k in d                # Check all nested values
    len[d]                # Number of nested values
    d.items[]             # All nested items
    dict[d]               # Flatten into a regular dictionary
    
    873

lớp bộ sưu tập. OrderedDict[[items]]

Trả về một thể hiện của lớp con

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6 có các phương thức chuyên dùng để sắp xếp lại thứ tự từ điển

Mới trong phiên bản 3. 1

popitem[cuối cùng=True]

Phương thức

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
857 cho các từ điển có thứ tự trả về và xóa một cặp [khóa, giá trị]. Các cặp được trả về theo thứ tự LIFO nếu giá trị cuối cùng là đúng hoặc thứ tự FIFO nếu sai

move_to_end[phím , cuối cùng=True]

Di chuyển một khóa hiện có đến một trong hai đầu của từ điển đã đặt hàng. Mục được di chuyển đến cuối 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

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
68 nếu khóa không tồn tại

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
1

Mới trong phiên bản 3. 2

Ngoài các phương pháp ánh xạ thông thường, các từ điển có thứ tự cũng hỗ trợ phép lặp ngược bằng cách sử dụng

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
877

Các bài kiểm tra bình đẳng giữa các đối tượng

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4 nhạy cảm với thứ tự và được triển khai dưới dạng
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
879. Kiểm tra bình đẳng giữa các đối tượng
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4 và các đối tượng
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
881 khác không phân biệt thứ tự như từ điển thông thường. Điều này cho phép các đối tượng
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4 được thay thế ở bất kỳ nơi nào sử dụng từ điển thông thường

Đã thay đổi trong phiên bản 3. 5. Các mục, khóa và giá trị lượt xem của

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4 hiện hỗ trợ lặp ngược bằng cách sử dụng
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
877.

Đã thay đổi trong phiên bản 3. 6. Với việc chấp nhận PEP 468, thứ tự được giữ lại cho các đối số từ khóa được truyền cho hàm tạo

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4 và phương thức
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
51 của nó.

Đã thay đổi trong phiên bản 3. 9. Đã thêm toán tử hợp nhất [

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
57] và cập nhật [
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
58], được chỉ định trong PEP 584.

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4 Ví dụ và Công thức¶

Thật đơn giản để tạo một biến thể từ điển theo thứ tự ghi nhớ thứ tự các phím được chèn lần cuối. Nếu một mục mới ghi đè lên một mục hiện có, vị trí chèn ban đầu sẽ được thay đổi và di chuyển đến cuối

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
2

Một

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
4 cũng sẽ hữu ích để triển khai các biến thể của
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
891

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
3

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
4

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
6 đối tượng¶

Lớp

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
6 hoạt động như một trình bao bọc xung quanh các đối tượng từ điển. Nhu cầu về lớp này đã được thay thế một phần bằng khả năng phân lớp trực tiếp từ
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
6;

lớp bộ sưu tập. UserDict[[dữ liệu ban đầu]]

Lớp mô phỏng từ điển. Nội dung của phiên bản được lưu giữ trong một từ điển thông thường, có thể truy cập được thông qua thuộc tính

c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
895 của phiên bản
class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
6. Nếu dữ liệu ban đầu được cung cấp, thì
c = ChainMap[]        # Create root context
d = c.new_child[]     # Create nested child context
e = c.new_child[]     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals[]
e.maps[-1]            # Root context -- like Python's globals[]
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x'] = 1            # Set value in current context
d['x']                # Get first key in the chain of contexts
del d['x']            # Delete from current context
list[d]               # All nested values
k in d                # Check all nested values
len[d]                # Number of nested values
d.items[]             # All nested items
dict[d]               # Flatten into a regular dictionary
895 được khởi tạo với nội dung của nó;

Ngoài việc hỗ trợ các phương thức và hoạt động của chuỗi, phiên bản

class DeepChainMap[ChainMap]:
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__[self, key, value]:
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__[self, key]:
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError[key]

>>> d = DeepChainMap[{'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}]
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap[{'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'}]
8 còn cung cấp thuộc tính sau

Python dict có thứ tự hay không có thứ tự?

Kể từ phiên bản Python 3. 7, từ điển được đặt hàng. Trong Python 3. 6 trở về trước, từ điển không có thứ tự . Khi chúng tôi nói rằng từ điển được sắp xếp theo thứ tự, điều đó có nghĩa là các mục có thứ tự xác định và thứ tự đó sẽ không thay đổi.

Python Dicts có được đặt hàng ngay bây giờ không?

Điều này đã thay đổi trong Python 3. 6. Lớp dict tích hợp sẵn hiện cũng sắp xếp các mục của nó . Do đó, nhiều người trong cộng đồng Python hiện đang tự hỏi liệu OrderedDict có còn hữu ích không.

Các mục dict có được đặt hàng không?

Nói chung từ điển không được sắp xếp . Chúng được nhóm theo cặp khóa/giá trị. Chúng thường nhanh khi chúng được ánh xạ băm. Nếu bạn biết khóa, bạn có thể nhanh chóng nhận được giá trị của nó.

Chủ Đề