replacing numbers in LARGE MATRIX with criterium in 2 columns (a--> b)
MRAB
google at mrabarnett.plus.com
Mon May 4 09:54:33 EDT 2009
Alexzive wrote:
> Hello,
> I have this matrix [20*4 - but it could be n*4 , with n~100,000] in
> file "EL_list" like this:
>
> 1, 1, 2, 3
> 2, 4, 1, 5
> 3, 5, 1, 6
> 4, 7, 5, 6
> 5, 8, 7, 9
> 6, 8, 5, 7
> 7, 10, 9, 7
> 8, 10, 11, 12
> 9, 7, 13, 10
> 10, 14, 15, 16
> 11, 14, 17, 15
> 12, 17, 14, 18
> 13, 13, 16, 10
> 14, 16, 15, 11
> 15, 16, 11, 10
> 16, 19, 20, 21
> 17, 22, 15, 20
> 18, 17, 20, 15
> 19, 23, 20, 24
> 20, 25, 24, 20
>
> I would like to replace some numbers in "EL_list" but only in columns
> 2,3,4 using the criterium by line in file "criterium" (column 1 are
> the new numbers, column 2 the old ones).
>
> 1 1
> 2 3
> 3 5
> 4 12
> 5 13
> 6 14
> 7 15
> 8 16
> 9 17
> 10 18
> 11 19
> 12 10
> 13 21
> 14 22
> 15 23
> 16 24
> 17 25
>
> For example all the 7 have to replaced by 15 and so on..
>
> -- How to implement it in a fast end efficient code?
>
Read the contents of "criterium" into a dict with the old value as the
key and the new value as the value and then iterate through the matrix
replacing the values in columns 2 to 4 with criterium.get(current_value,
current_value).
Incidentally, the singular of "criteria" is "criterion" because it's
Greek, not Latin.
More information about the Python-list
mailing list