1

I'm trying to do 3 slices from a list of vectors from a txt file, with 2 columns, using conditions to separate the RGB.

But when I run the program the following error appears: "'float' object is not iterable". Can anyone help me?

#File opening
arq = open('arquivo.txt','r')

lines = arq.readlines()
arq.close()

vetor_x = []
vetor_y = []
lista_geral = []

for line in lines:
    line = line.replace('\n','')
    line = line.strip()
    line = line.replace(',','.')
    line = line.split('\t')

    if(len(line) == 2):
        X = line[0]
        Y = line[1]
        vetor_x.append(float(X))
        vetor_y.append(float(Y))
        lista_geral.append([X,Y])

#Conditions
B = 0
G = 0
R = 0

for i in range(0,len(vetor_x)):

    if vetor_x[i] <= 500:
        vetor_xB[B] = list(vetor_x[i])
        vetor_yB[B] = list(vetor_y[i])
        B += 1

    elif vetor_x[i] <= 600:
        vetor_xG[G] = list(vetor_x[i])
        vetor_yG[G] = list(vetor_y[i])
        G += 1

    elif vetor_x[i] <= 700:
        vetor_xR[R] = list(vetor_x[i])
        vetor_yR[R] = list(vetor_y[i])
        R += 1

print('####### vetor_xB #######')
print(vetor_xB)
print('####### vetor_yB #######')
print(vetor_yB)
print('####### vetor_xG #######')
print(vetor_xG)
print('####### vetor_yG #######')
print(vetor_yG)
print('####### vetor_xR #######')
print(vetor_xR)
print('####### vetor_yR #######')
print(vetor_yR)

However, when I try to run it, this causes this error:

Traceback (most recent call last):
  File "teste4.py", line 30, in <module>
    vetor_xB[B] = list(vetor_x[i])
TypeError: 'float' object is not iterable

Please help me!

1
  • 1
    vetor_xB[B] = list(vetor_x[i]) you are trying to typecast a single value of vetor_x. Apparently its a float. Commented Jan 2, 2019 at 19:43

3 Answers 3

2

The error means that this line vetor_xB[B] = list(vetor_x[i]), is not able to convert the float values to a list, instead you probably want to simply append the float values to your lists, but we need to define those lists before we can use them, like this:

# Define the lists we want to append the data to
vetor_xB = []
vetor_yB = []
vetor_xG = []
vetor_yG = []
vetor_xR = []
vetor_yR = []

for i in range(len(vetor_x)):
    if vetor_x[i] <= 500:
        vetor_xB.append(vetor_x[i])
        vetor_yB.append(vetor_y[i])

    elif vetor_x[i] <= 600:
        vetor_xG.append(vetor_x[i])
        vetor_yG.append(vetor_y[i])

    elif vetor_x[i] <= 700:
        vetor_xR.append(vetor_x[i])
        vetor_yR.append(vetor_y[i])

Then your output should be as you expect:

####### vetor_xB #######
[400.0, 401.10373, 409.54288, 425.24905, 442.71341, 470.96112, 488.7771, 499.79489, 499.91208, 500.0]
####### vetor_yB #######
[46.5713, 42.8136, 103.12, 12.6044, 262.43, 680.958, 171.28, 39.846, 41.1616, 39.2299]
####### vetor_xG #######
[523.70581, 540.81854, 579.26355, 593.3288, 600.0]
####### vetor_yG #######
[38523.1, 31554.2, 243.866, 90.6795, 212.789]
####### vetor_xR #######
[602.58838, 618.64624, 630.01563, 655.33307, 669.74994, 686.74548, 698.11487, 700.0]
####### vetor_yR #######
[251.602, 367.089, 3568.19, 44230.4, 7747.32, 768.864, 1229.72, 1808.8]

Note: Formatting of reading the file can be simplified, replace all of this:

for line in lines:
    line = line.replace('\n','')
    line = line.strip()
    line = line.replace(',','.')
    line = line.split('\t')

with just these 3 lines:

for line in lines:
    line = line.replace(',','.')
    line = line.strip().split()
Sign up to request clarification or add additional context in comments.

Comments

2

In this code

if vetor_x[i] <= 500:
    vetor_xB[B] = list(vetor_x[i])

it is clear from your if-test that you expect vetor_x[i] to be a number; and it is a number, because otherwise you would get a runtime error on the if-test.

But then you are calling list() on this number. So I suspect that list(x) does not do what you think it does. What it actually does is take all the elements of x (which could be a list or a tuple or a string or ...) and put them in a list. Suppose vetor_x[i] == 400.00000. Then your code is doing list(400.00000). That makes no sense.

I think you may intend

if vetor_x[i] <= 500:
    vetor_xB.append(vetor_x[i])

...but that is just a guess.

You can't make a Python list longer by assigning values to elements that don't exist. So if vetor_xB is an empty list, then assigning vetor_xB[B] = will fail, because irrespective of the value of B, there is no element B: if B == 0 then vetor_xB[B] is the same as vetor_xB[0], and trying to assign that will fail because there is no element zero.

So, to make the list longer, use append() instead. But to append to a list, the list must first exist. Your code doesn't provide a starting value for vetor_xB. At the top of the program there needs to be something like vetor_xB == [].

Comments

1

vetor_x is definitely iterable as shown when you define it in line 6. That leaves vetor_xB as the source of your error. I do not see it defined anywhere.

From your usage of vetor_xB, you could define it as a dictionary somewhere before your loop and call vetor_xB.values() to output your desired list.

Alternatively (and preferably), you can define vetor_xB as a list and instead of vetor_xB[B] you can write vetor_xB.append(B). This seems the simpler, more logical solution.

And even more preferably use pandas as below. This is a much quicker, more efficient solution and honestly removes much of the time taken to manually parse your data. Here entire document is replaced with 6 lines of code + some print statements. It might not be the most memory efficient, but it offers some significant advantages that I think are worth the switch.

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

for col in df.columns: df[col] = pd.to_numeric(df[col].str.replace(',','.'))

df.columns = ['X', 'Y']

vetores = {}
vetores['vetor_B'] = df.loc[df['X']<=500]
vetores['vetor_G'] = df.loc[(df['X']>500) & df['X']<=600]
vetores['vetor_R'] = df.loc[(df['X']>600) & df['X']<=700]

for vetor in vetores.keys():
    print(vetor+'X:')
    print(vetores[vetor]['X'])
    print(vetor+'Y:')
    print(vetores[vetor]['Y'])
    print('-'*40)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.