Hướng dẫn python speed up list search - python tăng tốc tìm kiếm danh sách

Bạn nên thay đổi headwordList thành set.

Bài kiểm tra word in headwordList sẽ rất chậm. Nó phải thực hiện một so sánh chuỗi trên mỗi từ trong headwordList, một từ tại một thời điểm. Nó sẽ mất thời gian tỷ lệ thuận với độ dài của danh sách; Nếu bạn tăng gấp đôi độ dài của danh sách, bạn sẽ tăng gấp đôi lượng thời gian cần thiết để thực hiện bài kiểm tra (trung bình).

Với set, luôn mất cùng một thời gian để làm bài kiểm tra def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 0; Nó không phụ thuộc vào số lượng các phần tử trong set. Vì vậy, đó sẽ là một tốc độ lớn.

Bây giờ, toàn bộ vòng lặp này có thể được đơn giản hóa:

for x in headwordList: m = SequenceMatcher(None, y.lower(), x) if m.ratio() > percentage: percentage = m.ratio() word = x if percentage > 0.86: sentenceList[count] = word

Tất cả những điều này làm là tìm từ từ headwordList có tỷ lệ cao nhất và giữ nó (nhưng chỉ giữ nó nếu tỷ lệ trên 0,86). Đây là một cách nhanh hơn để làm điều này. Tôi sẽ thay đổi tên headwordList thành def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 4 vì tôi muốn bạn làm cho nó là một set chứ không phải def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 6.

def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word)

Điều này có vẻ hơi khó khăn nhưng đó là cách nhanh nhất để làm điều này trong Python. Chúng tôi sẽ gọi hàm def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 7 tích hợp để tìm kết quả def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 8 có tỷ lệ cao nhất. Đầu tiên, chúng tôi xây dựng một "biểu thức máy phát" thử tất cả các từ trong def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 4, gọi def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time genexp = ((SequenceMatcher(None, y, word), word) for word in headwords) m, word = max(genexp, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 0 trên mỗi từ. Nhưng khi chúng tôi hoàn thành, chúng tôi cũng muốn biết từ này là gì. Vì vậy, biểu thức máy phát tạo ra các bộ dữ liệu, trong đó giá trị đầu tiên trong bộ tuple là kết quả def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 8 và giá trị thứ hai là từ. Hàm def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 7 không thể biết rằng những gì chúng ta quan tâm là tỷ lệ, vì vậy chúng ta phải nói điều đó; Chúng tôi làm điều này bằng cách thực hiện một chức năng kiểm tra những gì chúng tôi quan tâm, sau đó chuyển chức năng đó như đối số def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time genexp = ((SequenceMatcher(None, y, word), word) for word in headwords) m, word = max(genexp, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 3. Bây giờ def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 7 tìm thấy giá trị với tỷ lệ cao nhất đối với chúng tôi. def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 7 tiêu thụ tất cả các giá trị được tạo bởi biểu thức máy phát và trả về một giá trị duy nhất, sau đó chúng tôi giải nén vào các varaible def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time genexp = ((SequenceMatcher(None, y, word), word) for word in headwords) m, word = max(genexp, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 6 và def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time genexp = ((SequenceMatcher(None, y, word), word) for word in headwords) m, word = max(genexp, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 7.

Trong Python, tốt nhất là sử dụng các tên biến như def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time genexp = ((SequenceMatcher(None, y, word), word) for word in headwords) m, word = max(genexp, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 8 thay vì def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time genexp = ((SequenceMatcher(None, y, word), word) for word in headwords) m, word = max(genexp, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 9. Vui lòng xem các hướng dẫn này: //www.python.org/dev/peps/pep-0008/

Đó không phải là thực tế tốt để sử dụng một biến chỉ mục gia tăng và gán vào các vị trí được lập chỉ mục trong một danh sách. Thay vào đó, hãy bắt đầu với một danh sách trống và sử dụng hàm phương thức m, word = max(((SequenceMatcher(None, y, word), word) for word in headwords), key=check_ratio) 0 để nối các giá trị.

Ngoài ra, bạn có thể làm tốt hơn để xây dựng một từ điển từ và tỷ lệ của chúng.

Lưu ý rằng mã ban đầu của bạn dường như có lỗi: Ngay khi bất kỳ từ nào có tỷ lệ phần trăm trên 0,86, tất cả các từ được lưu trong def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time genexp = ((SequenceMatcher(None, y, word), word) for word in headwords) m, word = max(genexp, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 9 bất kể tỷ lệ của chúng là bao nhiêu. Mã tôi đã viết, ở trên, chỉ lưu các từ trong đó tỷ lệ của chính từ này đủ cao.

EDIT: Đây là để trả lời một câu hỏi về các biểu thức của trình tạo cần được đặt dấu ngoặc đơn.

Bất cứ khi nào tôi nhận được thông báo lỗi đó, tôi thường tự tách biểu thức trình tạo và gán nó cho một biến. Như thế này:

def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time genexp = ((SequenceMatcher(None, y, word), word) for word in headwords) m, word = max(genexp, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word)

Đó là những gì tôi đề xuất. Nhưng nếu bạn không bận tâm đến một dòng phức tạp trông thậm chí còn bận rộn hơn, bạn chỉ cần thêm một cặp dấu ngoặc đơn như thông báo lỗi cho thấy, vì vậy biểu thức máy phát được đặt dấu ngoặc đơn. Như vậy:

m, word = max(((SequenceMatcher(None, y, word), word) for word in headwords), key=check_ratio)

Python cho phép bạn bỏ qua các dấu ngoặc đơn rõ ràng xung quanh biểu thức máy phát khi bạn chuyển biểu thức đến một hàm, nhưng chỉ khi đó là đối số duy nhất cho hàm đó. Vì chúng tôi cũng đang vượt qua một đối số def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time genexp = ((SequenceMatcher(None, y, word), word) for word in headwords) m, word = max(genexp, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 3, chúng tôi cần một biểu thức máy phát dấu ngoặc đơn đầy đủ.

Nhưng tôi nghĩ việc đọc dễ dàng hơn nếu bạn chia ra GenExp trên dòng của chính nó.

Chỉnh sửa: @Peter Wood chỉ ra rằng tài liệu cho thấy việc sử dụng lại def check_ratio(m): return m.ratio() y = y.lower() # do the .lower() call one time m, word = max((SequenceMatcher(None, y, word), word) for word in headwords, key=check_ratio) percentage = max(percentage, m.ratio()) # remember best ratio if m.ratio() > 0.86: setence_list.append(word) 8 cho tốc độ. Tôi không có thời gian để kiểm tra điều này, nhưng tôi nghĩ đây là cách đúng đắn để làm điều đó.

Hạnh phúc, mã trở nên đơn giản hơn! Luôn luôn là một dấu hiệu tốt.

Chỉnh sửa: Tôi vừa kiểm tra mã. Mã này làm việc cho tôi; Xem nếu nó làm việc cho bạn.

from difflib import SequenceMatcher headwords = [ # This is a list of 650,000 words # Dummy list: "happy", "new", "year", ] def words_from_file(filename): with open(filename, "rt") as f: for line in f: for word in line.split(): yield word def _match(matcher, s): matcher.set_seq2(s) return (matcher.ratio(), s) ratios = {} best_ratio = 0 matcher = SequenceMatcher() for word in words_from_file("sentences.txt"): matcher.set_seq1(word.lower()) if word not in headwords: ratio, word = max(_match(matcher, word.lower()) for word in headwords) best_ratio = max(best_ratio, ratio) # remember best ratio if ratio > 0.86: ratios[word] = ratio print(best_ratio) print(ratios)

Cách nhanh nhất để tìm một yếu tố trong danh sách Python là gì?

Để tạo điều kiện cho điều này, Python có chức năng sẵn có gọi là index (). Hàm này lấy phần tử làm đối số và trả về chỉ mục. Bằng cách sử dụng chức năng này, chúng tôi có thể tìm thấy chỉ mục của một phần tử trong danh sách trong Python.index(). This function takes in the element as an argument and returns the index. By using this function we are able to find the index of an element in a list in Python.

Tìm kiếm trong Set nhanh hơn danh sách Python?

Nói chung các danh sách nhanh hơn các bộ.Nhưng trong trường hợp tìm kiếm một phần tử trong bộ sưu tập, các bộ nhanh hơn vì các bộ đã được thực hiện bằng cách sử dụng các bảng băm.Vì vậy, về cơ bản, Python không phải tìm kiếm toàn bộ, điều đó có nghĩa là độ phức tạp về thời gian trung bình là O (1).sets are faster because sets have been implemented using hash tables. So basically Python does not have to search the full set, which means that the time complexity in average is O(1).

Những gì nhanh hơn danh sách trong Python?

Danh sách được phân bổ trong hai khối: một khối cố định với tất cả thông tin đối tượng Python và một khối có kích thước biến cho dữ liệu.Đó là lý do tạo ra một tuple nhanh hơn danh sách.Nó cũng giải thích sự khác biệt nhỏ về tốc độ lập chỉ mục nhanh hơn danh sách, bởi vì trong các bộ đếm để lập chỉ mục, nó tuân theo ít con trỏ hơn.a tuple is faster than List. It also explains the slight difference in indexing speed is faster than lists, because in tuples for indexing it follows fewer pointers.

Tại sao được đặt tốt hơn danh sách?

Bởi vì các bộ không thể có nhiều lần xuất hiện của cùng một phần tử, nên nó làm cho các bộ rất hữu ích để loại bỏ hiệu quả các giá trị trùng lặp khỏi danh sách hoặc tuple và để thực hiện các hoạt động toán học phổ biến như các công đoàn và giao điểm., it makes sets highly useful to efficiently remove duplicate values from a list or tuple and to perform common math operations like unions and intersections.

Chủ đề