Từ điển Python nhanh hơn

Trong bài viết này, chúng ta sẽ thảo luận về lý do tại sao việc lặp qua lệnh lại chậm như vậy trong Python?

con trăn




import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
5

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
6
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
7

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
6
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
9

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
0

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
1

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
2
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
3

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
60______261
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
62
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
63
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
64

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
67
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
68
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
69
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
70
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
63
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
72

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
73
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
74
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
61
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
67

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
78
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
70
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
60
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
61

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
62
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
63____264
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
65
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
68
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
61

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
62
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
91____264

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
0

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
94

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
2
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
96

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
98
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
61
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
00
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
01

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
67
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
68
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
69
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
70
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
63
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
72

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
73
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
11
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
61
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
67

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
78
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
70
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
60
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
61

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
62
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
20
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
64
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
65
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
68
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
61

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
62
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
91____264

Trong Python Script ở trên, chúng ta có hai chức năng

  1. np_hiệu suất. Hàm này tạo một mảng NumPy trống cho 10.00.000 phần tử và lặp lại trên toàn bộ mảng cập nhật giá trị của phần tử riêng lẻ vào vị trí trình lặp [trong trường hợp này là 'i']
  2. dict_hiệu suất. Hàm này tạo một Từ điển trống cho 10.00.000 phần tử và lặp lại trên toàn bộ từ điển, cập nhật giá trị của từng phần tử vào vị trí trình vòng lặp [trong trường hợp này là 'i']

Và cuối cùng, một hệ thống. gọi hàm getsizeof[] để tính toán mức sử dụng bộ nhớ theo cấu trúc dữ liệu tương ứng

Bây giờ chúng ta gọi cả hai hàm này và để đo thời gian thực hiện của từng hàm, chúng ta sử dụng hàm %time cung cấp cho chúng ta Thời gian thực thi câu lệnh hoặc biểu thức Python. Hàm %time có thể được sử dụng như một phép thuật dòng và ô

  1. Ở chế độ nội tuyến, bạn có thể đặt thời gian cho câu lệnh một dòng [mặc dù nhiều câu lệnh có thể được xâu chuỗi bằng dấu chấm phẩy]
  2. Trong chế độ ô, bạn có thể bấm giờ cho phần thân ô [một câu lệnh ngay sau sẽ gây ra lỗi]

Gọi các hàm này từ chế độ nội tuyến bằng phương thức %time

Python3




import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
30

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
31
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
32

đầu ra

Python3




import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
30

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
31
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
35

đầu ra

Như chúng ta có thể thấy, có sự khác biệt khá lớn về thời gian Wall giữa việc lặp lại trên mảng NumPy và từ điển python

Tạo từ điển với các mục được sắp xếp và chỉ đọc, trả về giá trị mặc định cho các khóa không tồn tại, v.v.

Ảnh của oxana v trên BaptMotivation

Bạn có thể quen thuộc với một từ điển trong Python cho phép tra cứu, chèn và xóa hiệu quả bất kỳ giá trị nào được liên kết với một khóa nhất định. Nói một cách đơn giản, hãy tưởng tượng bạn vào một cửa hàng tạp hóa, muốn tìm giá của một mặt hàng nào đó, bạn chỉ cần nhập tên mặt hàng đó và giá của mặt hàng đó sẽ được trả về ngay lập tức.

food = {
'fish': 10,
'broccoli': 3,
'tofu': 5,
'mushroom': 3
}
food['fish']

kết quả.

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
0

Nhưng điều gì sẽ xảy ra nếu bạn muốn từ điển của mình hoạt động theo một cách cụ thể mà bạn cần phải suy nghĩ để thực hiện?

  • Tạo một từ điển theo thứ tự
  • Trả về các giá trị mặc định khi không có khóa được yêu cầu
  • Nhóm nhiều từ điển vào một ánh xạ duy nhất
  • Tạo từ điển chỉ đọc

Tạo một từ điển có thứ tự

Kịch bản

Bạn muốn sử dụng từ điển để sắp xếp các công việc mà bạn muốn hoàn thành vào cuối tuần. Các khóa từ điển là các nhiệm vụ, các giá trị là số giờ cần thiết để hoàn thành nhiệm vụ. Vì bạn muốn hoàn thành các nhiệm vụ theo thứ tự đầu vào — bạn không muốn từ điển trộn lẫn thứ tự các nhiệm vụ của mình — bạn quyết định sử dụng

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
1

Ra lệnhDict

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
2 là lý tưởng nếu thứ tự quan trọng đối với mục tiêu của bạn

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys

kết quả

OrderedDict[[['laundry', 0.5], ['shopping', 2], ['clean', 2], ['movie', 2]]]odict_keys[['laundry', 'shopping', 'clean', 'movie']]

Như chúng ta có thể thấy, các mục trong danh sách được liệt kê theo thứ tự đầu vào. Bây giờ bạn chỉ cần tra từ điển để hoàn thành công việc cuối tuần theo đúng thứ tự

Trả lại giá trị mặc định khi không có khóa được yêu cầu

Kịch bản

Bạn muốn tạo một từ điển ánh xạ các lớp tới số phòng. Vì nhiều lớp học được dạy bên ngoài [vì thời tiết đẹp], bạn không muốn dành thời gian để ánh xạ các lớp đó tới giá trị

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
3. Bạn quyết định sử dụng
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
4

mặc định

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
5 có thể được khởi tạo bằng một hàm không nhận đối số và cung cấp giá trị mặc định nếu không tìm thấy khóa được yêu cầu

from collections import defaultdictclasses = defaultdict[lambda: 'Outside']classes['Math'] = 'B23'
classes['Physics'] = 'D24'
classes['Math']

kết quả.

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
6.
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
7 hoạt động giống như một từ điển tiêu chuẩn, ngoại trừ nó cho phép trả về giá trị của khóa không tồn tại

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
2

kết quả.

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
3

Giá trị mặc định mà chúng tôi đã chỉ định được trả về khi khóa không tồn tại được cung cấp. Bây giờ bạn không cần phải dành thời gian để lập bản đồ 20 lớp được dạy bên ngoài thành 13 giá trị

Nhóm nhiều từ điển thành một ánh xạ duy nhất

Kịch bản

Bạn muốn ghi chú danh sách bạn bè và thông tin của họ. Thông tin của mỗi người bạn sẽ được biểu diễn dưới dạng từ điển có tên và tuổi. Khi bạn có thông tin về tất cả bạn bè của mình, bạn suy nghĩ làm thế nào để tập hợp tất cả thông tin đó vào một nơi duy nhất. Bạn khám phá

OrderedDict[[['laundry', 0.5], ['shopping', 2], ['clean', 2], ['movie', 2]]]odict_keys[['laundry', 'shopping', 'clean', 'movie']]
0

Bản Đồ Chuỗi

OrderedDict[[['laundry', 0.5], ['shopping', 2], ['clean', 2], ['movie', 2]]]odict_keys[['laundry', 'shopping', 'clean', 'movie']]
1 cư xử như tên của nó. Cấu trúc dữ liệu cho phép bạn nhóm nhiều từ điển lại với nhau như một chuỗi

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
7

kết quả

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
8

Vậy làm thế nào để từ điển tìm kiếm một khóa được cung cấp?

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
9

kết quả.

OrderedDict[[['laundry', 0.5], ['shopping', 2], ['clean', 2], ['movie', 2]]]odict_keys[['laundry', 'shopping', 'clean', 'movie']]
2

Tạo từ điển chỉ đọc

Kịch bản

Như bạn đã biết, từ điển cho phép người dùng truy cập và thay đổi các mục trong từ điển. Điều gì sẽ xảy ra nếu bạn chỉ muốn sử dụng từ điển để cung cấp cho người dùng thông tin về khóa mà không thay đổi nó? . Sau đó, bạn nên cân nhắc sử dụng

OrderedDict[[['laundry', 0.5], ['shopping', 2], ['clean', 2], ['movie', 2]]]odict_keys[['laundry', 'shopping', 'clean', 'movie']]
3

Lập bản đồProxyType

OrderedDict[[['laundry', 0.5], ['shopping', 2], ['clean', 2], ['movie', 2]]]odict_keys[['laundry', 'shopping', 'clean', 'movie']]
3 cung cấp chế độ xem chỉ đọc đối với dữ liệu của từ điển được bao bọc. Điều này không khuyến khích người dùng chỉnh sửa từ điển

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
3

kết quả

import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys
4

Như bạn có thể thấy, bây giờ các sinh viên không còn có thể thay đổi phòng để gây nhầm lẫn cho các bạn cùng lớp của họ

Phần kết luận

Xin chúc mừng. Nếu bạn đã xem đến cuối bài viết này, bạn sẽ biết những triển khai từ điển nào khác có sẵn và khi nào nên sử dụng chúng. Có kiến ​​thức này sẽ hữu ích khi bạn muốn từ điển của mình thực hiện các chức năng cụ thể

Vì vậy, bạn có nên chuyển đổi từ điển tiêu chuẩn với các triển khai từ điển đặc biệt này không? . Thông thường, bạn sẽ thấy mình làm tốt với từ điển tiêu chuẩn. Lưu bài viết này và quay lại bài viết này khi bạn cần triển khai từ điển theo cách khác. Vui lòng rẽ nhánh và chơi với mã cho bài viết này trong kho lưu trữ Github này

Tôi thích viết về các khái niệm khoa học dữ liệu cơ bản và chơi với các thuật toán và công cụ khoa học dữ liệu khác nhau. Bạn có thể kết nối với tôi trên LinkedIn và Twitter

Gắn dấu sao cho repo này nếu bạn muốn kiểm tra mã cho tất cả các bài báo tôi đã viết. Theo dõi tôi trên Phương tiện để được thông báo về các bài báo khoa học dữ liệu mới nhất của tôi như thế này

dict[] hay {} nhanh hơn?

Như chúng ta có thể thấy, dict[] rõ ràng là chậm hơn {} . Đặc biệt nếu dictionary được khởi tạo nhiều phần tử thì ảnh hưởng rất lớn nếu code của bạn cần 0. 04ms hoặc gần như 0. 08ms để tạo từ điển của bạn. Ngay cả khi bạn khởi tạo một từ điển trống, nó vẫn chậm hơn.

Tuple hay từ điển nào nhanh hơn?

Ai cũng biết rằng trong Python bộ dữ liệu nhanh hơn danh sách và ký tự nhanh hơn đối tượng.

Từ điển hay DataFrame nào nhanh hơn?

Đối với một số mục đích nhỏ, được nhắm mục tiêu nhất định, lệnh có thể nhanh hơn . Và nếu đó là tất cả những gì bạn cần, thì chắc chắn hãy sử dụng một lệnh chính tả. Nhưng nếu bạn cần/muốn sức mạnh và sự sang trọng của DataFrame, thì dict không thể thay thế. So sánh tốc độ là vô nghĩa nếu cấu trúc dữ liệu trước tiên không đáp ứng nhu cầu của bạn.

Cái gì nhanh hơn từ điển Python?

Tra cứu nhanh hơn trong từ điển vì Python triển khai chúng bằng cách sử dụng bảng băm. Nếu chúng ta giải thích sự khác biệt bằng các khái niệm Big O, từ điển có độ phức tạp thời gian không đổi, O[1] trong khi danh sách có độ phức tạp thời gian tuyến tính, O[n].

Chủ Đề