python中的矩阵搜索单词
我需要构建一个函数,从字母矩阵中的列表中搜索单词, 如果单词存在,该函数将返回包含矩阵中单词的元组列表。每个元组中都有一个单词和它在矩阵中出现的次数。(单词,数字) 我被困住了,我需要敌人的帮助。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有不同的话,例如相同的单词,但计数不同(例如1和2),则已发布的代码将获得数量计数。
我认为您的意思是,同一单词出现在
矩阵的多行中,然后在每一行上出现为
+1
。如果我运行此示例:
使用帖子中给出的代码,我会看到
您想要的是:
[('abba',2)]
。在这里,我将保留一个单词及其在行上的频率,最后转换为元组列表,假设您需要从问题中以该格式的结果。
在我给出的示例上,这
是第一个,第二和第四行计的。对于它给出的评论中提供的示例:
哪个听起来像您想要的。
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:
with the code given in the post I see
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.
On the example I gave this gives
which counts the 1st, 2nd, and 4th row. For the example provided in a comment it gives:
which sounds like what you want.