python中的矩阵搜索单词

发布于 2025-01-19 09:35:33 字数 541 浏览 0 评论 0原文

我需要构建一个函数,从字母矩阵中的列表中搜索单词, 如果单词存在,该函数将返回包含矩阵中单词的元组列表。每个元组中都有一个单词和它在矩阵中出现的次数。(单词,数字) 我被困住了,我需要敌人的帮助。

def right(word_list, matrix):
    sum_lst = []
    one_word = [''.join(row) for row in matrix]
    for word in word_list:
        for string in one_word:
            print(string)
            count =string.count(word)
            if word in string:
                if (word,count) not in sum_lst:
                    sum_lst.append((word, count))



    return sum_lst

这让我回想起一个事实:一个单词出现一次,即使它实际上出现了不止一次。

I need to build a function that searches for words from a list within a matrix of letters,
If a word exists the function will return list of tupels with the words that in the matrix. and in every tuple tere is a word and the number of times it appears in the matrix.(word,number)
I an stuck and i would like foe help.

def right(word_list, matrix):
    sum_lst = []
    one_word = [''.join(row) for row in matrix]
    for word in word_list:
        for string in one_word:
            print(string)
            count =string.count(word)
            if word in string:
                if (word,count) not in sum_lst:
                    sum_lst.append((word, count))



    return sum_lst

It brings me back to the fact that a word appears once even if it actually appears more than once.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2025-01-26 09:35:33
It brings me back to the fact that a word appears once even if it actually appears more than once.

如果有不同的话,例如相同的单词,但计数不同(例如1和2),则已发布的代码将获得数量计数。

我认为您的意思是,同一单词出现在矩阵的多行中,然后在每一行上出现为+1

如果我运行此示例:


matrix =[['a', 'b', 'b', 'a'], 
         ['a', 'b', 'b', 'a', 'a', 'b', 'b', 'a'], 
         ['e', 'f', 'g'], 
         ['e', 'f', 'e', 'a', 'b', 'b', 'a', 'a', 'b', 'b', 'a', 'h']]

words = ['abba']
result = right(words, matrix)
result

使用帖子中给出的代码,我会看到

>>> result
[('abba', 1), ('abba', 2)]

您想要的是:[('abba',2)]

在这里,我将保留一个单词及其在行上的频率,最后转换为元组列表,假设您需要从问题中以该格式的结果。

def right(words, matrix):
    word_occur = dict()
    one_word = [''.join(row) for row in matrix]
    for word in words:
        if word in one_word:
            word_occur[word] = word_occur.get(word,0) + 1
    return [item for item in word_occur.items()]

在我给出的示例上,这

[('abba', 3)]

是第一个,第二和第四行计的。对于它给出的评论中提供的示例:

[('app', 2), ('nner', 1)]

哪个听起来像您想要的。

It brings me back to the fact that a word appears once even if it actually appears more than once.

The posted code will grab word counts if the differ, e.g. the same word but different counts (say 1 and 2).

I think here what you mean is that the same word appears in multiple rows of the matrix then occurrence on each row counts as a +1.

If I run this example:


matrix =[['a', 'b', 'b', 'a'], 
         ['a', 'b', 'b', 'a', 'a', 'b', 'b', 'a'], 
         ['e', 'f', 'g'], 
         ['e', 'f', 'e', 'a', 'b', 'b', 'a', 'a', 'b', 'b', 'a', 'h']]

words = ['abba']
result = right(words, matrix)
result

with the code given in the post I see

>>> result
[('abba', 1), ('abba', 2)]

whereas what it sounds like you want is: [('abba', 2)].

Here I'd keep a dictionary of the words and the frequency of their occurrence on rows and at the end convert into the list of tuples, assuming you need results in that format from question.

def right(words, matrix):
    word_occur = dict()
    one_word = [''.join(row) for row in matrix]
    for word in words:
        if word in one_word:
            word_occur[word] = word_occur.get(word,0) + 1
    return [item for item in word_occur.items()]

On the example I gave this gives

[('abba', 3)]

which counts the 1st, 2nd, and 4th row. For the example provided in a comment it gives:

[('app', 2), ('nner', 1)]

which sounds like what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文