Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've made a function to search horizontally for a word in a matrix full of letters. Now I'm trying to reuse this code in order to find any word in matrix. I need to read the matrix line by line, compare the words made with my list of words (like a dictionary) and if the word exists in the list, a word has been found. This is my initial code:

def search(p):  
    x=matrix() #matrix of letters
    for line in x:
        if p in ''.join(line):
            return True

I've written some other code but none of it works. I've been looking for similar questions but none of them answer my question.

This is the code I have and doesn't work:

def auto_search():
    l=[] #list of words found
    x=matrix()
    for line in x:
        for i in ''.join(line):
            if search_dic(i)!=-1: #searchs in the list of words if the word is there
                l.append(i)
    print (l)

For example, a have this matrix:

[[a,p,e,n],
 [g,h,j,k],
 [e,r,l,d]]

The function has to read the matrix and find the word "pen" by itself. Any help would be appreciated. Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
290 views
Welcome To Ask or Share your Answers For Others

1 Answer

def in_matrix(matrix, target):

    for my_list in matrix:
        my_str = ''.join(my_list)

        if target in my_str:
            return True 

...

def search_matrix(matrix, targets):
    found = []

    for row in matrix:
        my_str = ''.join(row)

        for target in targets:
            try:
                my_str.index(target)  #Throws ValueError if target not in my_str
                found.append(target)  #This line skipped if ValueError
            except ValueError:
                pass

    return found



targets = ['pen', 'dog', 'horse']

matrix = [
    ['p','x','e','n'],
    ['g','d','o','g'],
    ['p','e','n','d']
]

print search_matrix(matrix, targets)

--output:--
['dog', 'pen']

...

import itertools as it

def word_in_permutations(target_word, permutations):
    for permutation in permutations:
        my_str = ''.join(permutation)

        try:
            my_str.index(target_word)
            return True
        except ValueError:
            pass

    return False



def search_matrix(matrix, target_words):
    all_found = []

    for row in matrix:
        row_found = []

        for target_word in target_words:
            my_iter = it.permutations(row)

            if(word_in_permutations(target_word, my_iter)):
                row_found.append(target_word)

        all_found.append(row_found)

    return all_found



targets = ['pen', 'dog', 'pig']

matrix = [
    ['n','e','d','g','o','p'],
    ['g','o','d','g','i','p'],
]

print search_matrix(matrix, targets)

--output:--
[['pen', 'dog'], ['dog', 'pig']]

There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements.

Python Lists: The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. Good thing about a list is that items in a list need not all have the same type.

http://www.tutorialspoint.com/python/python_lists.htm


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...