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.keys5
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys6
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys7
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys6
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys9
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys0
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys1
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys2
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys3
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys60______261
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys62
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys63
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys64
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys67
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys68
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys69
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys70
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys63
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys72
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys73
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys74
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys61
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys67
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys78
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys70
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys60
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys61
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys62
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys63____264
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys65
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys68
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys61
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys62
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys91____264
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys0
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys94
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys2
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys96
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys98
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys61
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys00
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys01
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys67
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys68
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys69
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys70
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys63
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys72
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys73
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys11
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys61
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys67
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys4
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys78
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys70
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys60
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys61
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys62
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys20
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys64
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys65
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys66
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys68
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys61
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys62
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys91____264
Trong Python Script ở trên, chúng ta có hai chức năng
- 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']
- 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à ô
- Ở 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]
- 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.keys30
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys31
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys32
đầu ra
Python3
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys30
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys31
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys35
đầ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.keys0
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.keys1
Ra lệnhDict
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys2 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.keys3. Bạn quyết định sử dụng
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys4
mặc định
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys5 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.keys6.
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys7 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.keys2
kết quả.
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys3
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ấtKị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.keys7
kết quả
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys8
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.keys9
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.keys3
kết quả
import collectionstasks = collections.OrderedDict[laundry=0.5, shopping=2, clean=2]tasks['movie'] = 2taskstasks.keys4
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