user2930168
user2930168

Reputation: 13

Python-Location of a letter in a two dimensional string

I am working on creating a playfair cipher for python and I am having trouble indexing the location of a letter in the table provided below.

     [['A', 'B', 'C', 'D', 'E'],
     ['F', 'G', 'H', 'I', 'Y'],
     ['K', 'L', 'M', 'N', 'O'],
     ['P', 'Q', 'R', 'S', 'T'],
     ['U', 'V', 'W', 'X', 'Z']]

I was wondering how I would be able to find the location of a letter in the table which gives an output of row and column.

I've looked online for different solutions, but I can't seem to make it work properly.

Upvotes: 1

Views: 157

Answers (6)

Shashank
Shashank

Reputation: 13869

Here is my weird way. :) Note: Python 2.7 so / means integer division.

table = [['A', 'B', 'C', 'D', 'E'],
         ['F', 'G', 'H', 'I', 'Y'],
         ['K', 'L', 'M', 'N', 'O'],
         ['P', 'Q', 'R', 'S', 'T'],
         ['U', 'V', 'W', 'X', 'Z']]

tablestring = ''.join(''.join(row) for row in table)
x = tablestring.index('V')
i = x / (len(tablestring) / len(table))
j = x % (len(tablestring) / len(table))
print i, j
print table[i][j]

Prints:

4 1
V

Upvotes: 1

user2555451
user2555451

Reputation:

My take:

>>> lst = [['A', 'B', 'C', 'D', 'E'],
...        ['F', 'G', 'H', 'I', 'Y'],
...        ['K', 'L', 'M', 'N', 'O'],
...        ['P', 'Q', 'R', 'S', 'T'],
...        ['U', 'V', 'W', 'X', 'Z']]
>>> get = "S"
>>> {x:y.index(get) for x,y in enumerate(lst) if get in y}
{3: 3}
>>> get = "V"
>>> {x:y.index(get) for x,y in enumerate(lst) if get in y}
{4: 1}
>>>

Upvotes: 3

dawg
dawg

Reputation: 103814

Here is another way:

matrix=[['A', 'B', 'C', 'D', 'E'],
     ['F', 'G', 'H', 'I', 'Y'],
     ['K', 'L', 'M', 'N', 'O'],
     ['P', 'Q', 'R', 'S', 'T'],
     ['U', 'V', 'W', 'X', 'Z']]

def index(letter, matrix):
    for i,li in enumerate(matrix):
        try:
            j=li.index(letter)
            return i,j
        except ValueError:
            pass    

    raise ValueError("'{}' not in matrix".format(letter))

print index('H', matrix)     
# (1, 2) 
print index('a', matrix) 
# ValueError

Upvotes: 0

Joel Green
Joel Green

Reputation: 920

from itertools import product

li = [['A', 'B', 'C', 'D', 'E'],
     ['F', 'G', 'H', 'I', 'Y'],
     ['K', 'L', 'M', 'N', 'O'],
     ['P', 'Q', 'R', 'S', 'T'],
     ['U', 'V', 'W', 'X', 'Z']]

letter = 'P'

for i, j in product(range(len(li)),range(len(li[0]))):
    if li[i][j] == letter:
        print (i,j)
        break

Upvotes: 1

thefourtheye
thefourtheye

Reputation: 239463

data = [['A', 'B', 'C', 'D', 'E'],
     ['F', 'G', 'H', 'I', 'Y'],
     ['K', 'L', 'M', 'N', 'O'],
     ['P', 'Q', 'R', 'S', 'T'],
     ['U', 'V', 'W', 'X', 'Z']]

search = "I"

for rowIdx, row in enumerate(data):
    if search in row:
        print rowIdx, row.index(search)
        break

Output

1 3

Upvotes: 1

Michael0x2a
Michael0x2a

Reputation: 64038

Is this what you're looking for?

def find_index(table, letter):
    for r_index, row in enumerate(table):
        if letter in row:
            return (r_index, row.index(letter))

You iterate through the table, and search for the index of the letter in each row.


Alternatively, if you're constantly searching in the matrix, it might be more efficient to convert it to a dict so you get O(1) access:

def get_index_map(table):
    output = {}
    for r_index, row in enumerate(table):
        for c_index, letter in enumerate(row):
            output[letter] = (r_index, c_index)
    return output

Then, you can just call this function once at the start of your program, and use the returned dict to find the row and column number of each letter.

Upvotes: 4

Related Questions