Search through a 2-dimensional list without numpy

前端 未结 4 1225
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 03:41

I\'m looking to define a function that accepts two parameters: an int and a list.

If the function finds the integer in the list it returns

4条回答
  •  情书的邮戳
    2021-01-20 03:58

    I would used solution like this:

    #!/usr/bin/env ipython
    # ---------------------
    l = [
             [0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 2, 1, 1, 0, 1, 1, 1, 0],
             [0, 1, 0, 1, 0, 0, 0, 1, 0],
             [0, 1, 0, 1, 1, 1, 0, 1, 0],
             [0, 1, 0, 0, 0, 1, 0, 1, 0],
             [0, 1, 1, 1, 0, 1, 0, 1, 0],
             [0, 0, 0, 1, 0, 1, 0, 1, 0],
             [0, 1, 1, 1, 0, 1, 0, 1, 0],
             [0, 1, 0, 0, 0, 0, 0, 1, 0],
             [0, 1, 0, 1, 1, 1, 0, 1, 0],
             [0, 1, 0, 1, 0, 1, 0, 1, 0],
             [0, 1, 1, 1, 0, 1, 1, 4, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0]
        ]
    # ----------------------------------
    def search(value,listin):
        coords = [[ival,kkval] for ival,dd in enumerate(listin) for kkval,val in enumerate(dd) if val==value]
        return coords
    # ----------------------------------
    result = search(4,l)
    print result
    

    where I defined a function search, which can be used to search for certain value from an input list.

提交回复
热议问题