简体   繁体   中英

How do I set a variable to a index of a list?

l = [1,2,4]

for i in range(3):
    a = l[i]

im tryihng to do that above but it isn't working and it says 'builtin_function_or_method' object cannot be interpreted as an integer. can anyone say why this is and tell me how to fix this edit:There was something earlier in the code before this and it was because i was doing.lower and not.lower() sorry guys

Let's explain what your code does before solving it. Edit available at the bottom of the answer

for i in range(3):
   a = l[i]

What this does is creates a "range" of numbers from 0 to 2, however its supposed to go from 1 (or 0) to 3. Why? Computers have been trained to start counting from 0 instead of 1 like a normal human and subsequently they are 1 less. (This is a simplified one, there's a longer one that you'll learn over time) Now your 2nd line of code assigns the variable a the value of one of the items in the list l. Let's look at what value 'a' would be assigned during this time

1 (1st item)
2 (2nd item)
IndexError: Out of range error (there is no 3rd item)

So how do you solve this? One way is to add more items to your list l. So let's then add 2 more items into l (3 and 4) This is our variable l now

l = [1, 2, 3, 4]

Here's our output now

1 (1st item)
2 (2nd item)
3 (3rd item)

As you noticed, it skips the 4th item since we specified to only iterate over 3 items in the list. If you wanted to "iterate over a list" look no further..

Observe

for i in l:
    print(i)

This creates a for loop that goes over each item in the list l one by one from start to finish that allows you to see the current item in the variable i, In our code. it simply prints the variable i each time the for loop goes to the next item in the list.

1
2
3 
4

And simply stops once it reaches the end, Don't worry: we've all been there while learning code :)

UPDATE: Based on what you were saying, I'm assuming if you wanted to assign the variable a the 2nd place in the list 'l' you would use

a = l[1]

Yes to get the 2nd place you need to type 1. The same goes for accessing the 1st item, you change the l[1] with l[0]. This is because computers count from 0 instead of human's traditionally counting from 1

the code you wrote isn't even syntactically correct.

l = [1,2]

for i in range(len(l)):
    # you had no colon, so the first error you should have gotten is a syntax error.
    # the second error you would have gotten is a value error, because you try to set 'a' to values
    # that don't exist. a more dynamic way to do it anyways is to cycle through the
    # length of the list.
    a = l[i]

im not sure why you want to do this, as it will result in a = 2 . staying true to your question, the only reasonable way to do what you're asking is something as easy as this.

a = l[2] # or whatever index you're trying to get

you're method, even if it compiled correctly, would not have accomplished what you say you want.

as mentioned by 'meh' and 'shriakhilc', keep in mind that indexing starts at 0, so the list l would only have indexes of 0 and 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM