繁体   English   中英

如何遍历 python 中的单词搜索?

[英]How can I iterate through the wordsearch in python?

我正在尝试进行一个单词搜索,返回在单词搜索中找到的单词的起始行、起始列、结束行和结束列,其中单词是从我的字符串列表中提取的,我在这里称之为“单词”。 我试图首先找到水平匹配,这是我尝试过的但我被卡住了。

  def horizontal(labels, words):
            puzzle_word = []
            rows = labels.shape[0] # 15
            cols = labels.shape[1] # 15
        
            length = len(words)
            word = 0 # initialize word where word will be extracted from words 
    
        # replacing all the whitespaces:
            for line in labels:
                line = line.replace(' ', '')
                line = line.strip()
                if len(line) = 0:
                    for line in labels:
                        line = line.replace('\n', '')
                        line = line.lower()
                        words.append(line)
                        words = [x.upper() for x in words]
                        words = sorted(words)
    
        # iterate through the entire words list & extracting the words 1 by 1
            while word < length:
                for row in range(rows):
                    for column in range(columns):
                        character = labels[row][col] 

        # if the starting character matches with a word in the 2-D grid/array puzzle (called labels)
        # and width of all the columns of the grid - the character column to make sure the word will fit and not go over the column size
                        if word[0] == character and len(word) <= (cols-col):

        # how can I implement then look for the next character of the word and see if it matches the next character in the grid on the next column since we are finding horizontally now.

也许像:

  for index in word:
          while index < len(word):
           index += 1
                    ......................

我正在使用的给定文件示例,其中网格应该采用二维 numpy 网格数组的形式:

    J H C A T E H U C Y M J A L Q # cat is here
    N Q Q H K C B V T E X U F A E
    W O T A X E J K G N B P V M D
    C H C O U I R D P O F X X Z B
    Q M F R C P L P B H X K S L S
    H L I E A R F C Q V O H M D D
    K V F V P E A S Y Q P Z O J L
    H K Z N L T V G P C N G H D L
    R R Z V B S H D S M X Y T L B
    N A E L E P H A N T I S D N A # elephant is here
    O Y F D O G W E I L P X J R Z # dog is here
    Y D F C R W O S A U Z Y O T W
    K H M E E A L N C C G X L F B
    L Z F F S K Q I E L A R S S B
    X Z O H P D M J W Y C V D P A
   
words: ['cat', 'dog', 'elephant']

拆分字符串。 字符串中单词之间的公共分隔符是空格。 拆分返回一个数组。 数组中的每个元素都是一个单词。 使用 for 循环遍历数组中存在的单词。

一个可以帮助您入门的简单解决方案。

import numpy as np

labels = """
J H C A T E H U C Y M J A L Q
N Q Q H K C B V T E X U F A E
W O T A X E J K G N B P V M D
C H C O U I R D P O F X X Z B
Q M F R C P L P B H X K S L S
H L I E A R F C Q V O H M D D
K V F V P E A S Y Q P Z O J L
H K Z N L T V G P C N G H D L
R R Z V B S H D S M X Y T L B
N A E L E P H A N T I S D N A
O Y F D O G W E I L P X J R Z
Y D F C R W O S A U Z Y O T W
K H M E E A L N C C G X L F B
L Z F F S K Q I E L A R S S B
X Z O H P D M J W Y C V D P A
""".strip()

lines = np.array([line.split(' ') for line in labels.split('\n')])
words = ['cat', 'dog', 'elephant']

# horizontal matches
matches = []
for row, line in enumerate(lines):

    line = ''.join(line).lower()
    for word in words:
        if  (col := line.find(word)) >= 0:
           matches.append([word, (row, row), (col, col + len(word) - 1)])
    
print(matches) # word, (start row, end row), (start col, end col)

思路是将每一行看成一个字符串,然后做substring匹配。 对于垂直匹配,您只需转置数组即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM