Python列表中的隐藏单词

Python列表中的隐藏单词,python,list,Python,List,我有一个函数,它从一个文本字符串生成一个列表列表。我希望它在列表列表中找到一个单词,即“noir”,无论是按行还是按列,并返回该单词的坐标,如下所示: row\u start是单词第一个字母的行号 column\u start是单词第一个字母的列号 row\u end是单词最后一个字母的行号 column\u end是单词最后一个字母的行号 这是到目前为止我的代码 def checkio(text, word): rows = [] col = [] coordina

我有一个函数,它从一个文本字符串生成一个列表列表。我希望它在列表列表中找到一个单词,即“noir”,无论是按行还是按列,并返回该单词的坐标,如下所示:

  • row\u start
    是单词第一个字母的行号
  • column\u start
    是单词第一个字母的列号
  • row\u end
    是单词最后一个字母的行号
  • column\u end
    是单词最后一个字母的行号
这是到目前为止我的代码

def checkio(text, word):
    rows = []
    col = []
    coordinates = [] 
    word = word.lower()
    text = text.lower()
    text = text.replace(" ", "")
    text = text.split("\n")
    for item in text:
        rows.append([item]) #Creates a list of lists by appending each item in brackets to list.
上述函数的输出示例:

   [['hetookhisvorpalswordinhand:'], 
    ['longtimethemanxomefoehesought--'], 
    ['sorestedhebythetumtumtree,'], 
    ['andstoodawhilei**n**thought.'], 
    ['andasinuffishth**o**ughthestood,'], 
    ['thejabberwock,w**i**theyesofflame,'], 
    ['camewhifflingth**r**oughthetulgeywood,'], 
    ['andburbledasitcame!']]
在上述情况下,“noir”所在位置的坐标为[4,16,7,16]。 行开始是第4行 列开始是第16列 行尾为第7行 柱端为#16柱


这个词可以在水平和垂直方向上找到,这个词不能颠倒

不完全漂亮,但回答了这个问题:-)我冒昧地把这列成了一个字符串列表。这必须在代码之前完成

words = [
    'hetookhisvorpalswordinhand:',
    'longtimethemanxomefoehesought--',
    'sorestedhebythetumtumtree,', 
    'andstoodawhileinthought.',
    'andasinuffishthoughthestood,',
    'thejabberwock,witheyesofflame,', 
    'camewhifflingthroughthetulgeywood,',
    'andburbledasitcame!'
]

word = 'noir'

print [[row+1, line.find(word)+1, row+1, line.find(word)+len(word), line] for row, line in enumerate(words) if line.find( word ) >= 0]

words_transp = [''.join(t) for t in zip(*words)]

print [[line.find(word)+1, col+1, line.find(word)+len(word), col+1, line] for col, line in enumerate( words_transp ) if line.find( word ) >= 0]
输出为:

[[4, 16, 7, 16, 'sotnoira']]
注意,没有太多的错误检查。作业练习:-)


顺便说一句,请注意,您必须小心计数,因为python从0开始,因此其中有“+1”。

如果您添加了一个示例列表和一个示例输出,这会很有帮助。此外,隐藏字是否可以在列表列表中多次出现,如果是,您会找到所有隐藏字还是只找到第一个隐藏字,那么你也在垂直地寻找单词?(就像在单词搜索游戏中一样?)这个单词可以倒转吗?你的列表是方形的吗(虽然可能不重要)?我添加了一个示例列表和示例输出,该单词在列表中只出现一次,是的,我正在垂直和水平搜索列表中的单词。不清楚你到底需要什么,你能详细说明吗?