mardi 23 février 2021

Using random.choice based on condition

I have a file with like this with 10000 rows:

         Col1   Col2        
1        G1     h5
2        G2     t5
3        JG     d2
4        G2     d2
5        G1     D5
6        GG     d2

I want to replace the string in col2 based on the outcome of random.choice each time it finds the pattern. for example if I find d2 in col2 I would want to use random.choice("a2","b2","g2") and want something like below:

        Col1   Col2        
1        G1     h5
2        G2     t5
3        JG     a2
4        G2     b2
5        G1     D5
6        GG     g2

So each time a pattern match occurs it has to print a random choice.

I have tried using for loop followed by if condition. but for some reason, I am getting it wrong.




Aucun commentaire:

Enregistrer un commentaire