Retrieving list list values in python

6

I've tried several things and still can not figure out what the problem with my code is, it's a simple code. With numpy I put the txt values in the arrays array and I want to make some copies for two other lists, mensagem and no .

    import numpy as dados
    i=0
    filename = input("Entre com o arquivo de dados:")

    entrada = dados.genfromtxt(filename, delimiter="    ", dtype=int)

    mensagem  = dados.zeros((300,1))
    no = dados.zeros((300,1))

    for x in range(0,entrada.shape[0]):
         if(entrada[x][0] in mensagem):
              print("JÁ")   
         else:
              mensagem[i]=(entrada[x][0])
              no[i]=(entrada[x][1])
              i=i+1

    arquivo = open('mensagem.txt', 'w')
    arquivo1 = open('no.txt', 'w')

    for x in range(0,300):
         arquivo.write(str(mensagem[x]))
         arquivo.write('\n')
    for x in range(0,300):
         arquivo1.write(str(no[x]))
         arquivo1.write('\n')

I have tried to change dtype to int , float , None . The list no always receives the correct value in a few places, and the rest it fills in with number 1.

It is worth mentioning that my data file that fills the list is of the format:

    2.94946 14  5
    2.92017 14  8
    2.9751  14  19
    2.97217 14  17
    2.88794 14  2
    2.95166 14  13
    2.87769 14  12
    2.95166 14  5
    2.95166 14  7
    2.88354 14  21
    2.94653 12  24
    2.99927 12  25

and has more than 300 thousand lines.

Any suggestions, or did you notice something wrong?

    
asked by anonymous 22.09.2015 / 19:05

1 answer

1

What I think is happening is that, the way you wrote it, your data is being imported as strings.

You have also defined mensagem and no as two-dimensional, where each element is a separate vector. There, in that line ...

no[i]=(entrada[x][1])

... you try to assign one of your columns (a value string , as "14" ) to a vector. Because strings behave like character vectors, you end up with no[i] = ['1', '4'] . I believe you were trying to create a tuple of 1 element. To do this you need to add a comma to distinguish the tuple from unnecessary parentheses:

no[i]=(entrada[x][1],)

Anyway, for me it worked:

# importando com os defaults do numpy. Os valores serão tratados como floats
entrada = dados.genfromtxt('import-numpy-data.txt')

# depois, mais embaixo, a vírgula que falei
no[i] = (entrada[x][1],)

# no final, na hora de salvar, você precisa extrair o elemento 0 de
# cada linha
arquivo1.write(str(no[x][0]))

With these changes my result using the data you passed was like this

14.0
14.0
14.0
14.0
14.0
14.0
14.0
14.0
12.0
12.0
0.0
0.0
0.0
0.0
0.0
...o resto é tudo 0.0 ...
    
23.09.2015 / 21:10