TransWikia.com

Is there a better way to write this snippet in python?

Stack Overflow Asked by Paulo Sergio Schlogl on December 11, 2021

I have a numpy vector of letters and a matrix of values like this:

vec = array(["A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V"], dtype="<U1")

matrix = array(
    [
        [5.0, -2.0, -1.0, -2.0, -1.0, -1.0, -1.0, 0.0, -2.0, -1.0, -2.0, -1.0, -1.0, -3.0, -1.0, 1.0, 0.0, -3.0, -2.0, 0.0],
        [-2.0, 7.0, -1.0, -2.0, -4.0, 1.0, 0.0, -3.0, 0.0, -4.0, -3.0, 3.0, -2.0, -3.0, -3.0, -1.0, -1.0, -3.0, -1.0, -3.0],
        [-1.0, -1.0, 7.0, 2.0, -2.0, 0.0, 0.0, 0.0, 1.0, -3.0, -4.0, 0.0, -2.0, -4.0, -2.0, 1.0, 0.0, -4.0, -2.0, -3.0],
        [-2.0, -2.0, 2.0, 8.0, -4.0, 0.0, 2.0, -1.0, -1.0, -4.0, -4.0, -1.0, -4.0, -5.0, -1.0, 0.0, -1.0, -5.0, -3.0, -4.0],
        [-1.0, -4.0, -2.0, -4.0, 13.0, -3.0, -3.0, -3.0, -3.0, -2.0, -2.0, -3.0, -2.0, -2.0, -4.0, -1.0, -1.0, -5.0, -3.0, -1.0],
    ]
)

And I got this way to use them for doing a dict of dict like this:

struct = {}

for i, char in enumerate(vec):
    struct[char] = {}
    for j, char2 in enumerate(vec):
        struct[char][char2] = matrix[i, j]

struct = {
    "A": {"A": 5.0, "R": -2.0, "N": -1.0, "D": -2.0, "C": -1.0, "Q": -1.0, "E": -1.0, "G": 0.0, "H": -2.0, "I": -1.0, "L": -2.0, "K": -1.0, "M": -1.0, "F": -3.0, "P": -1.0, "S": 1.0, "T": 0.0, "W": -3.0, "Y": -2.0, "V": 0.0},
    "R": {"A": -2.0, "R": 7.0, "N": -1.0, "D": -2.0, "C": -4.0, "Q": 1.0, "E": 0.0, "G": -3.0, "H": 0.0, "I": -4.0, "L": -3.0, "K": 3.0, "M": -2.0, "F": -3.0, "P": -3.0, "S": -1.0, "T": -1.0, "W": -3.0, "Y": -1.0, "V": -3.0},
    "N": {"A": -1.0, "R": -1.0, "N": 7.0, "D": 2.0, "C": -2.0, "Q": 0.0, "E": 0.0, "G": 0.0, "H": 1.0, "I": -3.0, "L": -4.0, "K": 0.0, "M": -2.0, "F": -4.0, "P": -2.0, "S": 1.0, "T": 0.0, "W": -4.0, "Y": -2.0, "V": -3.0},
    "D": {"A": -2.0, "R": -2.0, "N": 2.0, "D": 8.0, "C": -4.0, "Q": 0.0, "E": 2.0, "G": -1.0, "H": -1.0, "I": -4.0, "L": -4.0, "K": -1.0, "M": -4.0, "F": -5.0, "P": -1.0, "S": 0.0, "T": -1.0, "W": -5.0, "Y": -3.0, "V": -4.0},
    # ...
}

Is there a more pythonic or better way do do that?

3 Answers

With help from good people here I did that:

import numpy as np

    def read_matrix(filename1, filename2):

        """This function receive two files as input
        filename1: file with a list symbols that represent a kind of sequence
        filename2: file with integer values
        It returns a dictionary of dictionary as final structure."""

        alphabet = np.loadtxt(filename1, dtype=str)
        blosum_matrix = np.loadtxt(filename2, dtype=float)
        return {aac1: {aac2: blosum_matrix[i,j] for j, aac2 in enumerate(alphabet)} 
                for i, aac1 in enumerate(alphabet)}

print(read_matrix(alphabet_file, blossom_file))

Answered by Paulo Sergio Schlogl on December 11, 2021

You should post this to stack overflow's code review forum. But since you're here, your current code is fine as it is, I just have a few comments:

You could also rename your variables to be more meaningful, we already know the type of matrix and vec because of their instantiation, try naming them with something close to their real life meaning.

You could use list/dict comprehensions but be careful to keep it readable.

Finally you should wrap your code in functions if it's not already done :)

Answered by Taek on December 11, 2021

Comprehensions would be more Pythonic:

struct = {char: {char2: matrix[i, j] for j, char2 in enumerate(vec)}
           for i, char in enumerate(vec)}

Answered by Serge Ballesta on December 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP