从字符串列表中查找单词 [英] Finding a word from a list of strings

查看:85
本文介绍了从字符串列表中查找单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个字符串列表

["rdfa", "gijn", "ipqd"]

,并且有一个包含字符串"and"的变量,我如何检查列表中是否包含"and"?为了更清楚地说明此列表,将其视为单词搜索:

and have a variable containing the string "and", how would I be able to check if "and" is in the list? To make this more clear think of the list as a word search:

rdfa
gijn
ipqd

我看到了垂直的单词和",但是如何查看列表中的单词和"呢?查找水平词要容易得多,但是查找垂直词会使我感到困惑.我可能在想,如果我需要查找"and"的第一个字母是否在列表中的任何元素中,那么我需要查找第二个字母是否与第一个字母在同一列中,并且也在该行中在第一个字母的上方或下方,后续字母也一样(因为我希望此方法适用于任何长度的单词).但是我不确定如何实现.我希望这个问题很清楚,因为如果不显示单词搜索就很难解释.

I see the vertical word "and", but how would I be able to check if the word "and" is in the list? Finding horizontal words was much easier, but finding a vertical word is confusing me. I was thinking possibly that I would need to find if the first letter of "and" is in any element in the list, then I would need to find if the second letter is in the same column as the first, and also in the row above or below the first letter, and the same for subsequent letters (as I'd like this to work for any length word). However I'm not sure how this would be implemented. I hope the question is clear as it's quite difficult to explain without a showing a word search.

推荐答案

一种纯Python方式来转置字母矩阵将是

A pure python way to transpose your matrix of letters would be

def transpose(l):
    return map(''.join, zip(*l))

l = ["rdfa", "gijn", "ipqd"]

if 'and' in transpose(l):
    ...

这篇关于从字符串列表中查找单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆