Recovering list values from python lists

Asked

Viewed 638 times

7

I have tried several things and I still can not grasp what the problem of my code, is a simple code. With numpy i place txt values in the array of arrays and want to make some copies to 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’ve tried to change dtype for int, float, None. Always the list no receives in a few positions the correct value, and the rest fills with number 1.

It is worth saying that my data file that fills the list is of 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 noticed anything wrong?

  • your file is bounded by spaces or tabs? the lines even start with spaces like in your example?

  • Hello sergiopereira, yes, are fired by tabs...2.946TAB14TAB5

  • change delimiter to t made no difference :/

  • I think I found your problem. See my answer.

1 answer

2

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

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

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

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

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

Anyway, for me it worked with:

# 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 ...

Browser other questions tagged

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