Reputation: 7
Suppose i have a matrix like this
table = [
# grid: 4 by 9
['A','B','C','D'],
['E','F','G','H'],
['I','J','K','L'],
['M','N','O','P'],
['Q','R','S','T'],
['U','V','W','X'],
['Y','Z','1','2'],
['3','4','5','5'],
['7','8','9','0'],
]
If i want to print the the string thats two down on the third column(2x,3y) resulting in G. Or something along the lines. How do i tell python that it should be a grid? And how do i return list information, the table.find(something) did not work (saying table has no find attribute) Im fairly new to python. I have searched the internet, with not much help..
edit: I must be doing something wrong?
table = [
# grid: 4 by 9
# 1 2 3 4
['A','B','C','D'],#1
['E','F','G','H'],#2
['I','J','K','L'],#3
['M','N','O','P'],#4
['Q','R','S','T'],#5
['U','V','W','X'],#6
['Y','Z','1','2'],#7
['3','4','5','5'],#8
['7','8','9','0'],#9
]
print table[1][2], table[4][3]
Prints O and T. O is right, but T is not, thats row 5 isnt it?'
I'm trying to write a text positional encryption algorithm with text matrixes, like one of the famous ciphers( i cant remember the name of).
I want to apply the said printing of each letter to the text that is caught by raw_input, i used dictionaries before, but i want to try this row/column method if possible, it will be much harder to break.
Upvotes: 0
Views: 14845
Reputation: 142126
Are you referring to http://en.wikipedia.org/wiki/Vigenere_cipher ?
If you want to work with matrices, then numpy
is recommended (a simple example):
>>> import numpy as np
>>> a = np.array(table)
>>> a
array([['A', 'B', 'C', 'D'],
['E', 'F', 'G', 'H'],
['I', 'J', 'K', 'L'],
['M', 'N', 'O', 'P'],
['Q', 'R', 'S', 'T'],
['U', 'V', 'W', 'X'],
['Y', 'Z', '1', '2'],
['3', '4', '5', '5'],
['7', '8', '9', '0']],
dtype='|S1')
>>> print a[1,2]
G
>>> np.where(a=='G')
(array([1]), array([2]))
There's also an oft over looked string method that can be used to substitute text (however you decide to do it)...
>>> from string import maketrans
>>> trans = maketrans('AB', 'XY') # A->X, B->Y, everything else unchanged
>>> 'ABC'.translate(trans)
'XYC'
Upvotes: 1
Reputation: 80346
List indexing starts at zero, so for example a fourth element in a list has index 3. You can define a helper function to get items/columns by their "actual position".
def column(matrix, i):
return [row[i-1] for row in matrix]
column(table,2)
Out[15]:
['B', 'F', 'J', 'N', 'R', 'V', 'Z', '4', '8']
def getitem(matrix,row,column):
return matrix[row-1][column-1]
getitem(table,2,3)
Out[16]:
'G'
As for your edit, table[1][2]
should print G
, not O
and table[4][3]
rightly returns T
.
Upvotes: 1
Reputation: 428
table = [
['A','B','C','D'],
['E','F','G','H'],
['I','J','K','L'],
['M','N','O','P'],
['Q','R','S','T'],
['U','V','W','X'],
['Y','Z','1','2'],
['3','4','5','5'],
['7','8','9','0'],
]
# grid 4 by 9, 4 column, 9 row
rownum=3
colnum=2
print(table[rownum-1][colnum-1])
Upvotes: 1
Reputation: 526573
table[1][2]
would give the value in the second row, third column (since indices start at 0).
More specifically, what you're specifying is a list of lists, so table[1]
would resolve to ['E','F','G','H']
(the second item in the overall list), and then taking the third element of that with [2]
would give you 'G'
.
Upvotes: 2