Print np.array, formatting

Asked

Viewed 108 times

-2

Full class:

class Tupla:

def __init__(self, keys, palavras):
    self.keys = list(keys)
    self.palavras = list(palavras)
    self.op = dict(zip(self.keys, self.palavras))

def __getitem__(self, key):
    return self.op[key]

def __repr__(self):
    return f'Tabela Completa: {self.op}'

def get_tupla(self, key):
    return f'Palavra: {self.op[key]}'

Where Keys and words are two np.arrays.

dados = pd.read_csv('C:/Users/afons/Downloads/words.txt', error_bad_lines=False)

Creating array with all the words of the file through numpy and shuffling

listaPalavras = np.array(dados.values)
np.random.shuffle(listaPalavras)

Creating array with Keys and shuffling

listaIndices = np.array(range(len(listaPalavras)))
np.random.shuffle(listaIndices)

Creating the table of tuples

tabela = Tupla(listaIndices, listaPalavras)
  • Why create the array if pd.read_csv('C:/Users/afons/Downloads/words.txt', error_bad_lines=False) gives you back a Dataframe. Do print(dados) here comes all the information obtained from the CSV.

  • But the question is how to print the complete "self.op" which is a Dict, where I join the words of "data" with a random key from another list. In the tuple class ...

1 answer

1

To listaPalavrasis a numpy object and not a list. To make it possible to print twine-twisting Voce has to access the values of this column:

print(listaPalavras['twine-twisting'])

  • Take a look at this here: https://repl.it/repls/SubduedWellgroomedWebpages. Even though it is a numpy object it can be printed as a whole without having to reference portions of the object.

  • The example Voce sent in the link is a dictionary and a simple array being added in numpy.array. I don’t know what kind of data structure your dados.values.

  • That is the point, I am calling attention that we do not answer the author of the question until he clarifies it and provide more data. You may be correct, but you can’t say what the problem is with 100% certainty.

  • For it is, when I refer to a portion of the object it prints correctly, the problem if it gives when printing it as a whole, I will reformulate the question, with the complete class, I believe that by changing the function "pr(self):" whether possible, or not ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.