Junho Lee
Junho Lee

Reputation: 21

reference difference between 'in range()' usage and 'element in elements' usage within for loop in python3

I'm currently reading a dummy.txt, the content showing as below:

8t1080 0.077500 0.092123 -0.079937
63mh9j 0.327872 -0.074191 -0.014623
63l2o3 0.504010 0.356935 -0.275896
64c97u 0.107409 0.021140 -0.000909

Now, I am reading it using python like below:

lines = open("dummy.txt", "r").readlines()

I wanted to make a structure so that I can have a list... or array (doesn't matter) of arrays. Each smaller array will have the 0th element as string, and the following decimals will be a float. In order to do that, I'm currently trying:

for line in lines:
    line = line.split()
    for x in range(1, len(line)):
        line[x] = float(line[x])

Interestingly, this doesn't work, since

for line in lines:
    line = line.split()

wouldn't actually split the line, and change the read data (lines variable, to be specific).

Meanwhile, below works, and successfully modifies the read data (lines variable).

for x in range(0, len(lines)):
    lines[x] = lines[x].split()
    for x in range(1, len(line)):
        line[x] = float(line[x])

So what is the difference between the two for loop that has two different results?

Upvotes: 2

Views: 59

Answers (3)

Breno Baiardi
Breno Baiardi

Reputation: 99

In your first case it IS working, however each time the for loops the line variable is reset to the next value, and its current value is lost to recieve the next one.

aux=[]
for line in lines: #here the program changes the value of line
    line = line.split() # here you change the value of line
    for x in range(1, len(line)):
        line[x] = float(line[x])
    aux.append(line)

using an auxiliar var you can "save" your values to later use

Upvotes: 0

jc1850
jc1850

Reputation: 1086

You just need a data structure to output to for the first example i.e.

data = []
lines = open("dummy.txt", "r").readlines()
for line in lines:
    line = line.split()
    for x in range(1, len(line)):
        line[x] = float(line[x])
    data.append(line)

The data list will contain what you want.

Upvotes: 1

sacuL
sacuL

Reputation: 51335

You would greatly benefit from pandas in this case:

import pandas as pd

df = pd.read_csv('dummy.txt', sep=' ', header=None)

>>> df.values

array([['8t1080', 0.0775, 0.092123, -0.079937],
       ['63mh9j', 0.327872, -0.074191, -0.014622999999999999],
       ['63l2o3', 0.5040100000000001, 0.356935, -0.27589600000000003],
       ['64c97u', 0.10740899999999999, 0.02114, -0.000909]], dtype=object)

Or all in one go (without saving it your text file as a dataframe object):

my_array = pd.read_csv('dummy.txt', sep=' ', header=None).values

>>> my_array
array([['8t1080', 0.0775, 0.092123, -0.079937],
       ['63mh9j', 0.327872, -0.074191, -0.014622999999999999],
       ['63l2o3', 0.5040100000000001, 0.356935, -0.27589600000000003],
       ['64c97u', 0.10740899999999999, 0.02114, -0.000909]], dtype=object)

Upvotes: 1

Related Questions