For this question I will simplify what I am doing, say this is my array
food = ['chicken', 'banana', 'milk', 'OJ', 'pork', 'apple']
now I want to print off the list of what type of food my objects are, fruit, meat or drink. Can I?
for i in food:
if i==1 or i==5:
print("Fruit")
elif i==0 or i==4:
print("meat")
else:
print("drink")
However when I do this it just prints out
drink
drink
drink
drink
drink
drink
How do I get it to print out?
meat
fruit
drink
drink
meat
fruit
CodePudding user response:
When you use for i in food
it just returns every element of the list. To return the index use the combination of len
and range
for i in range(len(food)):
if i==1 or i==5:
print("fruit")
elif i==0 or i==4:
print("meat")
else:
print("drink")
CodePudding user response:
Since you're looping over food
with a for i in food
loop, i
is going to be an item in the loop, which is a string. Since you're checking if i == 1
, etc., it's always going to return false because the i
is never going to be an int. It's always going to be a str
, since the only items in food
are str
s.
You can instead loop over enumerate(food)
instead of food
, so you can have i
(the index of the item) and item
(the item itself) for each loop iteration.
This code will work as you expect:
for i, item in enumerate(food):
if i==1 or i==5:
print("Fruit")
elif i==0 or i==4:
print("meat")
else:
print("drink")
If you don't plan to ever use item
, replace it with an undercore (_
), like this:
for i, _ in enumerate(food):
...
CodePudding user response:
When you do for i in <collection>:
it gives you the elements directly. In other words, i
is chicken
then banana
etc.
You could do for i in range(len(food):
so that i
will be the indices you are looking for.
Another option would be
for i, item in enumerate(food):
which will give you both the index and the actual item. You are not currently using the item itself, just its index, but if you need both at once in the future, this is the way to do it.
CodePudding user response:
Because you assign i
like for i in food
, on each iteration, i
will be the next element in food
, e.g. i
will be 'chicken'
, 'banana'
, 'milk'
, and so on. To see this in action, try adding a line print(i)
at the top of the for
loop's body.
It seems like you want i
to hold the value of the index you are currently on. To iterate over the indices of food
, you can do for i in range(len(food))
. The range
function will generate values from 0
to len(food) - 1
, inclusively. Check out this link to learn more about the range
function: https://docs.python.org/3/library/stdtypes.html#range
CodePudding user response:
As Mr.T has pointed out in the comments, you should try printing out i in your loop to see what i actually is. i is referring to each individual item in your list, not a number. If you were to print i in your for loop, it would print the value of each individual item in your list, not the index number. Meaning i is referring to 'chicken' the first time around the loop, not the index number 0. You could try something like:
food = ['chicken', 'banana', 'milk', 'OJ', 'pork', 'apple']
count = 0
for i in food:
if count==1 or count==5:
print("Fruit")
elif count==0 or count==4:
print("Meat")
else:
print("drink")
count = 1