2

HELP! Just when I get one thing working, something else doesn't work! Again, I'm sure it's simple to experienced eyes, but I'm struggling! This is the code where I generate my lists and the data for said lists.

#Frame Creation


allframes = []

for n in range (0, (workingframes*archnodes*3)):
    allframes.append(n)

frames = allframes

print frames



#Frame Population

for f in range (0, workingframes):

    if f<=(workingframes/2):

        for x in range (0, (archnodes)):
            frames[((archnodes*3)+f)].insert(((archnodes*3)+f), (archstartred[x]))
            frames[((archnodes*3)+f+workingframes)].insert(((archnodes*3)+f+workingframes),(archstartgrn[x]))
            frames[((archnodes*3)+f+workingframes*2)].insert(((archnodes*3)+f+workingframes*2),(archstartblu[x]))

        for y in range (0, nodesperframe):
            archstartred.pop()
            archstartgrn.pop()
            archstartblu.pop()
            archstartred.insert(0, backred)
            archstartgrn.insert(0, backgrn)
            archstartblu.insert(0, backblu)

    else:
        for y in range (0, nodesperframe):
            archstartred.pop(0)
            archstartgrn.pop(0)
            archstartblu.pop(0)
            archstartred.append(backred)
            archstartgrn.append(backgrn)
            archstartblu.append(backblu)

        for x in range (0, (archnodes)):
            frames[(archnodes*3)+f].insert((archnodes*3), (archstartred[x]))
            frames[(archnodes*3)+f+workingframes].insert(((archnodes*3)+1),(archstartgrn[x]))
            frames[(archnodes*3)+f+workingframes*2].insert(((archnodes*3)+2),(archstartblu[x]))

I keep getting this lovely error:

AttributeError: 'int' object has no attribute 'insert'

I plan on converting the list to an array after the list is populated so that I can have it manipulated the way it needs to be. I'm sure there's a much easier way to do this, but I don't know and I'm new to Python!

4
  • 3
    the answer by Levon is absolutely correct, but it's hard to tell what you're doing here. Maybe if you told us what this piece of code was supposed to accomplish we could help you to fix it as opposed to telling you why you're getting a particular error. Commented Jul 25, 2012 at 1:50
  • Please note that frames = allframes will not make a copy, so frames and allframes refer to the same list. If you need to make a copy, use frames = allframes[:] or frames = list(allframes). Commented Jul 25, 2012 at 2:29
  • This code is designed to populate RGB values to RGB lights. All information is numeric, there are no text strings or data. If the insert is causing the error, how do I populate [frames] with the other numeric data? Commented Jul 25, 2012 at 3:20
  • @SteveEdwards You have to call insert on the list, not an integer in the list - I updated my answer with some additional information about this, I hope it helps. Commented Jul 25, 2012 at 11:37

2 Answers 2

5

These expressions frames[((archnodes*3)+f)] (all 3 versions) evaluate to be integers and you are trying to call an insert() method on them which is the cause of your error.

f is an integer created in the enclosing for loop, and the way archnodes are used in the first for loop also implies they are numbers. frames is a list of numbers too created in your first loop, so you are indexing into a list of numbers and then trying to call the insert() method on the specific number in the list. As the error message states,

'int' object has no attribute 'insert'

the type int does not have an insert method.

Update re comment above:

You can add/remove/insert items to the list, but you can't add/remove/insert to an integer. See this brief tutorial/reference on lists. So for instance to insert an item into a list you'd use this methods/approach:

list.insert(index, obj)

so in your case

frames.insert(index, .. )

not

frames[index].insert(..)

I hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

"somehow evaluate to integers" -- because they're set that way in the first for loop.
0

If you want to have a list you need to setup the list object first, like this.

l = []
l.append(element)

If you just do the list manipulation before you do the l = [] you will have the problem you describe.

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.