Bộ sưu tập là một mô-đun python tích hợp cung cấp các kiểu dữ liệu vùng chứa hữu ích. Các kiểu dữ liệu vùng chứa cho phép chúng ta lưu trữ và truy cập các giá trị một cách thuận tiện. Nói chung, bạn sẽ sử dụng danh sách, bộ dữ liệu và từ điển. Tuy nhiên, trong khi xử lý dữ liệu có cấu trúc, chúng ta cần các đối tượng thông minh hơn
Trong bài viết này, tôi sẽ hướng dẫn bạn qua các cấu trúc dữ liệu khác nhau được mô-đun
#> 250
9 hỗ trợ, hiểu khi nào nên sử dụng chúng với các ví dụnội dung
- têntuple
- Tuple được đặt tên là gì
- Một cách khác để tạo một tên tuple
- Tại sao nên sử dụng têntuple trên từ điển
- Tạo một tên tuple từ từ điển python
- Cách thay thế một thuộc tính trong một tên tuple
- Phản đối
- mặc định
- Ra lệnhDict
- Điều gì xảy ra khi bạn xóa và chèn lại khóa trong OrderedDict
- Sắp xếp với OrderedDict
- Bản Đồ Chuỗi
- Điều gì xảy ra khi chúng ta có các khóa dự phòng trong ChainMap
- Cách thêm từ điển mới vào ChainMap
- Cách đảo ngược thứ tự từ điển trong ChainMap
- Danh sách người dùng
- chuỗi người dùng
- Người dùngDict
# Import the collections module
import collections
Hãy để chúng tôi bắt đầu với
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0Têntuple[] là gì
Bạn có thể nghĩ về
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0 theo hai cáchLà một phiên bản nâng cao của tuple. Hoặc như một cách nhanh chóng để tạo một con trăn
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
2 với các thuộc tính được đặt tên nhất địnhĐiểm khác biệt chính giữa
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
3 và # Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0 là. trong khi # Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
3 cho phép bạn truy cập dữ liệu thông qua các chỉ số, thì với # Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0, bạn có thể truy cập các phần tử bằng tên của chúngBạn thực sự có thể xác định tất cả các thuộc tính mà một
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0 có thể chứa và tạo nhiều phiên bản của nó. Cũng giống như cách bạn sẽ làm với các lớp họcVì vậy, về mặt chức năng, nó giống với ________ 22 hơn, mặc dù nó có tên là ________ 23
Hãy tạo một
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0 đại diện cho một 'phim' với các thuộc tính 'thể loại', 'xếp hạng' và 'diễn viên chính'# Import the collections module
import collections
3Bây giờ, bạn có thể truy cập bất kỳ chi tiết nào của phim bạn muốn bằng mã định danh. Nó khá tiện lợi và thân thiện với người dùng
# Import the collections module
import collections
4# Import the collections module
import collections
5Một cách khác để tạo một # Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
Ngoài ra, bạn có thể chuyển danh sách tên trường thay vì tên tệp được phân tách bằng dấu cách
Hãy để chúng tôi xem một ví dụ
# Import the collections module
import collections
7____18Các mục trong
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0 có thể được truy cập bằng cả chỉ mục cũng như mã định danh# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0
#> 250
Tại sao nên sử dụng têntuple trên từ điển
Ưu điểm chính của
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0 là chúng chiếm ít không gian/bộ nhớ hơn so với một từ điển tương đươngVì vậy, trong trường hợp dữ liệu lớn, các bộ dữ liệu có tên hiệu quả
Tôi sẽ chứng minh điều tương tự trong ví dụ dưới đây
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
4Thực thi đoạn mã trên, bạn thấy rằng bộ tên có kích thước '64', trong khi một từ điển chiếm các byte '240' lớn hơn nhiều. Đó là bộ nhớ nhỏ hơn gần gấp 4 lần
Bạn có thể tưởng tượng hiệu ứng khi mở rộng để xử lý một số lượng lớn các đối tượng như vậy
Tạo một # Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0 từ một từ điển python
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
Bạn có nhận thấy cách chúng tôi chuyển đổi một từ điển thành một bộ có tên bằng cách sử dụng toán tử
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
45 không?Tất cả những gì bạn cần làm là. trước tiên hãy xác định cấu trúc của
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0 và chuyển từ điển [# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
47] cho # Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0 đó làm đối số. Yêu cầu duy nhất là, khóa của # Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
49 phải khớp với tên trường của # Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
0# Import the collections module
import collections
30# Import the collections module
import collections
31Cách thay thế một thuộc tính trong một tên tuple
Điều gì xảy ra nếu giá trị của một thuộc tính phải được thay đổi?
Bạn cần cập nhật nó trong dữ liệu. Nó có thể được thực hiện đơn giản bằng cách sử dụng phương pháp
# Import the collections module
import collections
301# Import the collections module
import collections
32# Import the collections module
import collections
33Phản đối
Một đối tượng
# Import the collections module
import collections
302 được cung cấp bởi thư viện
#> 250
9Bạn có một danh sách một số số ngẫu nhiên. Nếu bạn muốn biết mỗi số xuất hiện bao nhiêu lần thì sao?
# Import the collections module
import collections
304 cho phép bạn tính toán tần số một cách dễ dàng. Nó không chỉ hoạt động với các số mà còn với bất kỳ đối tượng có thể lặp lại nào, như chuỗi và danh sáchCounter là lớp con của
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
49, dùng để đếm các đối tượng có thể bămNó trả về một từ điển với các phần tử là khóa và số lượng [không có phần tử nào xuất hiện] dưới dạng giá trị
VÍ DỤ
# Import the collections module
import collections
34# Import the collections module
import collections
35Hãy sử dụng Counter để tìm tần suất xuất hiện của từng ký tự trong một chuỗi
# Import the collections module
import collections
36# Import the collections module
import collections
37Như bạn đã thấy, chúng ta có thể xem có những phần tử nào và số lượng của chúng trong một chuỗi danh sách
Trong trường hợp bạn có một câu và bạn muốn xem số từ thì làm như thế nào?
Sử dụng hàm
# Import the collections module
import collections
306 để tạo danh sách các từ trong câu và chuyển nó cho # Import the collections module
import collections
307# Import the collections module
import collections
38# Import the collections module
import collections
39Cách tìm các phần tử phổ biến nhất bằng Counter
Bộ đếm rất hữu ích trong các ứng dụng thực tế
Đặc biệt khi bạn cần xử lý dữ liệu lớn và muốn tìm hiểu tần suất xuất hiện của một số phần tử. Hãy để tôi chỉ ra một số phương pháp rất hữu ích bằng cách sử dụng Bộ đếm
# Import the collections module
import collections
308Điều này trả về một danh sách 'n phần tử phổ biến nhất' cùng với số lượng của chúng theo thứ tự giảm dần
# Import the collections module
import collections
40# Import the collections module
import collections
41Phương pháp
# Import the collections module
import collections
309 có thể được sử dụng để in mục lặp lại nhiều nhất. Nó được sử dụng trong phân tích tần số# Import the collections module
import collections
42# Import the collections module
import collections
43Chúng ta có thể sử dụng to để tìm ký tự lặp lại nhiều nhất trong một chuỗi
# Import the collections module
import collections
44# Import the collections module
import collections
45Điều gì xảy ra nếu bạn không chỉ định 'n' khi sử dụng
# Import the collections module
import collections
310?Tất cả các phần tử là số lượng của chúng sẽ được in theo thứ tự giảm dần của số lượng của chúng
# Import the collections module
import collections
46____147Phương thức
# Import the collections module
import collections
311 trả về tất cả các phần tử có số đếm lớn hơn 0# Import the collections module
import collections
48# Import the collections module
import collections
49mặc định
Từ điển là một tập hợp các khóa và giá trị không có thứ tự
trong chìa khóa. cặp giá trị, khóa phải khác biệt và không thể thay đổi. Đó là lý do tại sao trong từ điển, danh sách không thể là khóa vì nó có thể thay đổi. Tuy nhiên, một bộ dữ liệu có thể là một khóa
# Import the collections module
import collections
50Làm thế nào defaultdict khác với dict
Nếu bạn cố gắng truy cập một khóa không có trong từ điển, nó sẽ đưa ra một lỗi
# Import the collections module
import collections
312. Trong khi đó, trong một # Import the collections module
import collections
313 nó không đưa ra một # Import the collections module
import collections
312Nó không báo lỗi phím. Nếu bạn truy cập một khóa không có, thì
# Import the collections module
import collections
313 sẽ trả về giá trị mặc địnhcú pháp.
# Import the collections module
import collections
316Khi chúng tôi truy cập một khóa không có, hàm
# Import the collections module
import collections
317 sẽ trả về một giá trị mặc định# Import the collections module
import collections
51# Import the collections module
import collections
52Nếu bạn thực hiện lệnh trên, nó không cung cấp cho bạn ________ 1312.
Trong trường hợp bạn muốn xuất giá trị cho khóa được yêu cầu không có, bạn có thể xác định chức năng của riêng mình và chuyển nó vào defaultdict.
Xem ví dụ bên dưới
# Import the collections module
import collections
53# Import the collections module
import collections
54Trong tất cả các cách khác, nó giống như một từ điển bình thường. Các lệnh cú pháp tương tự cũng được sử dụng cho defaultdict
Trên thực tế, có thể khắc phục
# Import the collections module
import collections
312 trong từ điển bằng cách sử dụng phương pháp # Import the collections module
import collections
320# Import the collections module
import collections
55# Import the collections module
import collections
56Ra lệnhDict
Một dict là một tập hợp các cặp giá trị khóa KHÔNG ĐƯỢC ĐẶT HÀNG. Tuy nhiên, một
# Import the collections module
import collections
321 duy trì THỨ TỰ mà các phím được chèn vàoNó là phân lớp của
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
49Tôi sẽ tạo một
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
49 thông thường và biến nó thành # Import the collections module
import collections
321 để cho bạn thấy sự khác biệt# Import the collections module
import collections
57# Import the collections module
import collections
58Trong một
# Import the collections module
import collections
321, ngay cả sau khi thay đổi giá trị của một số khóa nhất định, thứ tự vẫn giữ nguyên hoặc không thay đổi# Import the collections module
import collections
59# Import the collections module
import collections
70Điều gì xảy ra khi bạn xóa và chèn lại khóa trong OrderedDict
Khi một khóa bị xóa, thông tin về đơn đặt hàng của nó cũng bị xóa. Khi bạn nhập lại khóa, nó được coi là một mục nhập mới và thông tin đơn đặt hàng tương ứng được lưu trữ
# Import the collections module
import collections
71# Import the collections module
import collections
72Khi cắm lại khóa, nó được coi là một mục mới
# Import the collections module
import collections
73# Import the collections module
import collections
74Bạn có thể thấy chiếc xe đạp ở cuối cùng, thứ tự đã thay đổi khi chúng tôi xóa chìa khóa
Có một số lệnh hữu ích có thể được thực thi. Chúng tôi có thể thực hiện các chức năng sắp xếp theo nhu cầu
Sắp xếp với OrderedDict
Nếu bạn muốn sắp xếp các mục theo thứ tự giá trị tăng dần thì sao?
Sắp xếp các mục theo KEY [theo thứ tự tăng dần]
# Import the collections module
import collections
75# Import the collections module
import collections
76Sắp xếp các cặp theo VALUE [theo thứ tự tăng dần]
# Import the collections module
import collections
77# Import the collections module
import collections
76Sắp xếp từ điển theo độ dài của chuỗi khóa [theo thứ tự tăng dần]
# Import the collections module
import collections
79# Import the collections module
import collections
80Bản Đồ Chuỗi
ChainMap là kiểu dữ liệu vùng chứa lưu trữ nhiều từ điển.
Trong nhiều trường hợp, bạn có thể có các từ điển tương tự hoặc có liên quan, bạn có thể lưu trữ chúng chung trong một
# Import the collections module
import collections
326Bạn có thể in tất cả các mục trong
# Import the collections module
import collections
326 bằng cách sử dụng toán tử # Import the collections module
import collections
328. Mã dưới đây thể hiện điều tương tự# Import the collections module
import collections
81# Import the collections module
import collections
82Bạn có thể in các khóa của tất cả các từ điển trong sơ đồ chuỗi bằng chức năng
# Import the collections module
import collections
329# Import the collections module
import collections
83# Import the collections module
import collections
84Bạn có thể in các giá trị của tất cả các từ điển trong sơ đồ chuỗi bằng chức năng
# Import the collections module
import collections
330# Import the collections module
import collections
85# Import the collections module
import collections
86Điều gì xảy ra khi chúng ta có các khóa dự phòng trong ChainMap
Có thể 2 từ điển có cùng khóa. Xem một ví dụ dưới đây
# Import the collections module
import collections
87# Import the collections module
import collections
88Quan sát rằng 'màu đỏ' không được lặp lại, nó chỉ được in một lần
Cách thêm từ điển mới vào ChainMap
Bạn có thể thêm một từ điển mới vào đầu ChainMap bằng phương pháp
# Import the collections module
import collections
331. Nó được thể hiện trong đoạn mã dưới đây# Import the collections module
import collections
89# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
00Cách đảo ngược thứ tự từ điển trong ChainMap
Có thể đảo ngược thứ tự từ điển được lưu trữ trong ChainMap bằng hàm
# Import the collections module
import collections
332# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
01# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
02Danh sách người dùng
Hy vọng bạn đã quen thuộc với trăn
# Import the collections module
import collections
333s?Một
# Import the collections module
import collections
334 là kiểu dữ liệu vùng chứa giống như danh sách, là lớp trình bao bọc cho các # Import the collections module
import collections
333cú pháp.
# Import the collections module
import collections
336Bạn chuyển một danh sách bình thường làm đối số cho danh sách người dùng. Danh sách này được lưu trữ trong thuộc tính dữ liệu và có thể được truy cập thông qua phương pháp
# Import the collections module
import collections
337# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
03# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
04Việc sử dụng UserLists là gì
Giả sử bạn muốn nhân đôi tất cả các yếu tố trong một số danh sách cụ thể như một phần thưởng. Hoặc có thể bạn muốn đảm bảo rằng không thể xóa phần tử nào khỏi danh sách đã cho
Trong những trường hợp như vậy, chúng tôi cần thêm một 'hành vi' nhất định vào danh sách của mình, điều này có thể được thực hiện bằng UserLists
Ví dụ: Hãy để tôi chỉ cho bạn cách sử dụng
# Import the collections module
import collections
334 để ghi đè chức năng của một phương thức tích hợp. Đoạn mã dưới đây ngăn việc thêm một giá trị mới [hoặc nối thêm] vào danh sách________ 205 ________ 206Đoạn mã trên in thông báo
# Import the collections module
import collections
339 và không cho phép nối thêm. Điều này có thể hữu ích nếu bạn muốn đảm bảo rằng không ai có thể điền tên của họ sau một thời hạn cụ thể. Vì vậy, # Import the collections module
import collections
334 có thời gian thực rất hiệu quảchuỗi người dùng
Cũng giống như
# Import the collections module
import collections
341 là lớp bao bọc cho các # Import the collections module
import collections
333, # Import the collections module
import collections
343 là lớp bao bọc cho các # Import the collections module
import collections
344Nó cho phép bạn thêm chức năng/hành vi nhất định vào chuỗi. Bạn có thể chuyển bất kỳ đối số chuyển đổi chuỗi nào cho lớp này và có thể truy cập chuỗi bằng thuộc tính dữ liệu của lớp
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
07# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
08Như bạn có thể thấy trong ví dụ trên, số 765 đã được chuyển đổi thành chuỗi '765' và có thể được truy cập thông qua phương thức
# Import the collections module
import collections
345Cách thức và thời điểm UserString có thể được sử dụng
________ 1343 có thể được sử dụng để sửa đổi chuỗi hoặc thực hiện một số chức năng nhất định
Điều gì sẽ xảy ra nếu bạn muốn xóa một từ cụ thể khỏi tệp văn bản [ở bất kỳ đâu]?
Có thể, một số từ đã đặt sai vị trí và cần phải loại bỏ
Hãy xem một ví dụ về cách `UserString` có thể được sử dụng để xóa một số từ lẻ nhất định khỏi một chuỗi
# Create a dict and namedtuple with same data and compare the size
import random
import sys
# Create Dict
dicts = {'numbers_1': random.randint[0, 10000],'numbers_2':random.randint[5000,10000]}
print['Size or space occupied by dictionary',sys.getsizeof[dicts]]
# converting same dictionary to a namedtuple
data=namedtuple['data',['numbers_1','numbers_2']]
my_namedtuple= data[**dicts]
print['Size or space occupied by namedtuple',sys.getsizeof[my_namedtuple]]
09
#> 250
0Bạn có thể thấy rằng 'bút chì' và 'cục tẩy' đã bị xóa bằng lớp hàm
# Import the collections module
import collections
347Chúng ta hãy xem xét một trường hợp khác. Điều gì sẽ xảy ra nếu bạn cần thay thế một từ bằng một số từ khác trong toàn bộ tệp?
# Import the collections module
import collections
348 làm cho điều này dễ dàng hơn nhiều như hình dưới đây. Đoạn mã dưới đây thay thế một từ nhất định trong toàn bộ tệp văn bản bằng cách sử dụng ________ 1343Tôi đã định nghĩa một chức năng trong lớp để thay thế một từ nhất định bằng 'Chủ tịch' trong suốt
#> 250
1
#> 250
2Như bạn có thể thấy, 'Rajesh' được thay thế bằng 'Chủ tịch' ở khắp mọi nơi. Tương tự, UserStrings giúp bạn đơn giản hóa mọi quy trình
Người dùngDict
Nó là một lớp bao bọc cho từ điển. Cú pháp, hàm tương tự như UserList và UserString
cú pháp.
# Import the collections module
import collections
350Chúng tôi chuyển một từ điển làm đối số được lưu trữ trong thuộc tính dữ liệu của UserDict
#> 250
3____04Cách UserDict có thể được sử dụng
# Import the collections module
import collections
351 cho phép bạn tạo một từ điển được sửa đổi theo nhu cầu của bạn. Hãy xem một ví dụ về cách UserDict có thể được sử dụng để ghi đè chức năng của phương thức tích hợp. Đoạn mã dưới đây ngăn không cho cặp khóa-giá trị bị hủy
#> 250
5
#> 250
6Bạn sẽ nhận được thông báo RunTimeError. Điều này sẽ hữu ích nếu bạn không muốn mất dữ liệu
Điều gì sẽ xảy ra nếu một số khóa có giá trị rác và bạn cần thay thế chúng bằng nil hoặc '0'?
#> 250
7
#> 250
8Trường có giá trị rác đã được thay thế bằng 0. Đây chỉ là những ví dụ đơn giản về cách UserDict cho phép bạn tạo từ điển với chức năng cần thiết
Đây là tất cả các kiểu dữ liệu vùng chứa từ mô-đun bộ sưu tập. Chúng tăng hiệu quả lên rất nhiều khi được sử dụng trên các tập dữ liệu lớn
Sự kết luận
Tôi hy vọng bạn đã hiểu khi nào và tại sao nên sử dụng các kiểu dữ liệu vùng chứa ở trên. Nếu bạn có bất kỳ câu hỏi, xin vui lòng gửi nó trong các ý kiến