Cảm ơn bạn đã ghé thăm website của chúng tôi. Chúng tôi sử dụng các cookie thiết yếu để giúp chúng tôi hiểu và nâng cao trải nghiệm người dùng. Bằng cách nhấp vào “Chấp nhận”, bạn đồng ý với việc sử dụng TẤT CẢ các cookie
Chấp nhận Chính sách bảo mậtPython đang trở thành một ngôn ngữ lập trình phổ biến vì nó vừa mạnh mẽ vừa dễ học và rất tuyệt vời để khám phá các danh sách từ. Dưới đây là một số tập lệnh mẫu để giúp bạn bắt đầu
Tập lệnh rất mạnh và thường có thể thực hiện các tìm kiếm mà RegEx không thể, nhưng hãy nhớ rằng tập lệnh chỉ có thể tìm thấy kết quả từ danh sách từ của bạn. Các tìm kiếm với Finder của chúng tôi cũng bao gồm các từ điển bên ngoài
Nhấp vào bất kỳ tập lệnh mẫu nào bên dưới để sao chép tập lệnh đó vào khay nhớ tạm của bạn
1. Tìm các từ bắt đầu và kết thúc bằng cùng một chữ cái hoặc cùng một chuỗi các chữ cái
Đây là một tập lệnh đơn giản mà bạn có thể sử dụng làm mẫu để khám phá danh sách từ của riêng mình, cho dù bạn đã nhận nó từ chúng tôi hay bạn tự xây dựng nó
Khi bạn đã thiết lập môi trường Python trên máy tính của mình, bạn có thể sao chép và dán tập lệnh này, thay đổi câu lệnh "mở" để trỏ đến tệp văn bản của bạn và chạy tập lệnh đó để tìm tất cả các từ bắt đầu và kết thúc bằng "x" hoặc
Đó là một ví dụ rất cụ thể, nhưng để tìm được từ cần tìm, tất cả những gì bạn phải làm là thay dấu kiểm ở cuối [nếu từ. bắt đầu với. ] với logic phù hợp với mục tiêu của bạn
import sys # Get the search string from command line, or prompt if it's missing. pattern = sys.argv[1] if len[sys.argv] > 1 else input["Enter Start/End search string: "] # Bail if we didn't get a search string. pattern = pattern.strip[].lower[] if len[pattern] == 0: sys.exit[] # Open the text file in read-only mode. # If it isn't in current directory, specify full path using forward slash ["/"] not backslash. with open["XwiJeffChenList.txt"] as f: lines = f.readlines[] for line in lines: line = line.lower[].strip[] # strip[] removes spaces and newline characters from start and end parts = line.split[';'] word = parts[0] # Get the first part of the line, the word before ";" # score = int[parts[1]] # Score isn't used here but this is how you get it if word.startswith[pattern] and word.endswith[pattern]: print[word]
2. Tìm các từ tồn tại có hoặc không có hậu tố được chỉ định
Trò chơi ô chữ năm 2021 dựa trên ý tưởng về các từ có hoặc không có hậu tố ION. Sửa đổi này để chương trình tìm các từ có và không có bất kỳ hậu tố nào bạn nhập
import sys suffix = sys.argv[1] if len[sys.argv] > 1 else input["Enter suffix: "] suffix = suffix.strip[].lower[] suffixLength = len[suffix] if suffixLength == 0: sys.exit[] with open["XwiJeffChenList.txt"] as f: lines = f.readlines[] count = 0 fullWordList = [] for line in lines: line = line.lower[].strip[] parts = line.split[';'] fullWord = parts[0] fullWordList.append[fullWord] # All words get added to list # if the word ends with our suffix, see if the word without the suffix is already in the list. # Note, this assumes words in alphabetical order, eg, PASS would precede PASSION in the list. if fullWord.endswith[suffix]: truncatedWord = fullWord[: -suffixLength] if truncatedWord in fullWordList: print[truncatedWord + ":" + fullWord] count += 1 print[f'{count} found']
3. Từ được tạo ra bằng cách kết hợp các từ khác
Ô chữ năm 2022 kết hợp các từ có 4 chữ cái để xây dựng câu trả lời theo chủ đề gồm 16 chữ cái. Chương trình này tìm kiếm một danh sách từ cho tất cả các câu trả lời có thể như vậy. Các kiểu biến được khai báo. Điều đó không cần thiết đối với những chương trình ngắn hạn này, nhưng đó là một cách thực hành tốt
from typing import List count: int = 0 # number of matches found List4: List[str] = [] # collection of all the 4-letter words List16: List[str] = [] # collection of all the 16-letter words with open["XwiJeffChenList.txt"] as f: lines: List[str] = f.readlines[] # Read the file and collect all the 4- and 16-letter words for line in lines: line = line.lower[].strip[] parts: List[str] = line.split[';'] word: str = parts[0] # Get the first part of the line, the word before ";" if len[word] == 4: List4.append[word] elif len[word] == 16: List16.append[word] # Loop through all the 16-letter words. Break each down into quarters, # and print it out if each part is a known 4-letter word. for word16 in List16: part1: str = word16[0:4] part2: str = word16[4:8] part3: str = word16[8:12] part4: str = word16[12:16] if part1 in List4 and part2 in List4 and part3 in List4 and part4 in List4: print[f'{part1}-{part2}-{part3}-{part4} = {word16}'] count += 1 print[f'{count} found']
4. Tìm các từ khớp với mẫu RegEx
Tập lệnh này được xây dựng trên mẫu đầu tiên bằng cách thay thế thử nghiệm bắt đầu/kết thúc đơn giản bằng đối sánh RegEx
Bạn có thể khám phá RegEx trên trang Finder của chúng tôi và xem một số ví dụ về RegEx tại đây
import re, sys from typing import Pattern, List pattern: str = sys.argv[1] if len[sys.argv] > 1 else input["Enter RegEx: "] pattern = pattern.strip[] count: int = 0 if len[pattern] == 0: sys.exit[] # Compile the pattern into a RegEx Object, so it only has to be evaluated once. prog: Pattern[str] = re.compile[pattern.replace["$v", "[aeiou]"].replace["$c", "[^aeiou]"], re.IGNORECASE] with open["XwiJeffChenList.txt"] as f: lines: List[str] = f.readlines[] for line in lines: line = line.lower[].strip[] parts = line.split[';'] word: str = parts[0] if prog.search[word]: print[word] count += 1 print[f'{count} found']
5. Chọn những từ không chứa bất kỳ chữ cái nào bạn chỉ định
Chỉ định, nói, "aiouy" để hiển thị các từ có nguyên âm duy nhất [nếu có] là E
So sánh với các kết quả Finder này. *-AIOUY sử dụng OneLook và ^[^AIOUY]+$ sử dụng Regex
Bạn sẽ thay đổi tập lệnh như thế nào để chọn các từ chỉ chứa các chữ cái bạn chỉ định?
import sys eliminators = sys.argv[1] if len[sys.argv] > 1 else input["Enter letters to eliminate: "] eliminators = eliminators.strip[].lower[] if len[eliminators] == 0: sys.exit[] eliminationArray = list[eliminators] # convert eliminators to array of characters to check with open["XwiWordList.txt"] as f: lines = f.readlines[] for line in lines: line = line.lower[].strip[] parts = line.split[';'] word = parts[0] score = parts[1] if not any[x in line for x in eliminationArray]: print[line]
6. Sắp xếp danh sách từ theo điểm số và sau đó theo độ dài
Ví dụ này khác. Không có đầu vào của người dùng. Nó sắp xếp một bản sao danh sách từ của bạn để những từ có giá trị cao nhất ở trên cùng. Trong mỗi điểm số, các từ được sắp xếp theo độ dài và sau đó theo thứ tự bảng chữ cái trong mỗi nhóm điểm số/độ dài
Tôi không nói cách sắp xếp cụ thể này là hữu ích, nhưng nó cho thấy cách thực hiện
Nếu bạn sắp xếp lại danh sách từ của mình bằng tập lệnh Python, có thể bạn muốn chạy chương trình của mình từ dòng lệnh rồi "chuyển" nó sang một tệp văn bản mới. Mở Dấu nhắc Lệnh, điều hướng đến thư mục của bạn và nhập nội dung nào đó như. chủ yếu. py > MyNewTextFile. txt
# Define the class AnswerWord that, for each word in our word list, # encapsulates the word, the length of the word, and the score. class AnswerWord[object]: def __init__[self, word="", score=0]: self.word = word self.length = len[word] self.score = score AnswerWordArray = [] # This array will hold a collection of AnswerWord objects # Use our usual logic to loop through the file. For each line, create a new AnswerWord object # and add it to the array for later sorting. with open["XwiWordList.txt"] as f: lines = f.readlines[] for line in lines: line = line.lower[].strip[] parts = line.split[';'] AnswerWordArray.append[AnswerWord[parts[0], int[parts[1]]]] # Python sorting leaves order intact for entries not affected by the current sort criterion. # First sort words alphabetically. # Then sort by length, which leaves the entries grouped by length but still alphabetized # within each group. # Then sort by reverse score, so all the high value words are first, from shortest to longest. AnswerWordArray.sort[key=lambda k: k.word] AnswerWordArray.sort[key=lambda k: k.length] AnswerWordArray.sort[key=lambda k: k.score, reverse=True] for answerWord in AnswerWordArray: print[answerWord.word + ";" + str[answerWord.score]]
Phần còn lại là ở bạn, sự khéo léo và sáng tạo của bạn. Chúc vui vẻ
Nếu bạn nghiêm túc về việc học Python, XWord Info khuyên dùng PyCharm. Phiên bản "Cộng đồng" miễn phí hoạt động tốt