zoo_animals = [ ]
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
for name in zoo_animals:
print("The number ",zoo_animals.count(name)," in the list is ",name)
it gives me:
The number 1 in the list is Dog
The number 1 in the list is cat
The number 1 in the list is fish
The number 1 in the list is shark
What i wanted
The number 1 in the list is Dog
The number 2 in the list is cat
The number 3 in the list is fish
The number 4 in the list is shark
CodePudding user response:
If you read the documentation for the count function, it explains what it does - counts the occurrences of the parameter in the list.
You want the position / index of each item, not the count
for idx, name in enumerate(zoo_animals):
print(f"the animal at position {idx 1} in the list is {name}")
CodePudding user response:
List.count(item)
returns how many times it appears in the list, not where. You're looking for List.index(item)
(but this is 0-based, as in the first item has an index of 0 and the second item has an index of 1, so you will want to add 1 to this)
CodePudding user response:
Oh I actually found it is there something better?
zoo_animals = [ ]
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
for name in zoo_animals:
print("The number ", zoo_animals.index(name) 1 ," in the list is ",name)
CodePudding user response:
The count method of list returns the frequency or the number of times the provided elements has occurred in the list.
Using enumerate, we get the element and the index of it simultaneously.
zoo_animals = [ ]
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
for i, name in enumerate(zoo_animals):
print("The number ", i 1," in the list is ",name)
Output:
Enter an animal = aa
Enter an animal = bb
Enter an animal = cc
Enter an animal = dd
The number 1 in the list is aa
The number 2 in the list is bb
The number 3 in the list is cc
The number 4 in the list is dd
Check the below links, to know more about enumerate(<-list name->) function
https://docs.python.org/3/library/functions.html#enumerate
https://python-reference.readthedocs.io/en/latest/docs/functions/enumerate.html
Regards,
CodePudding user response:
In your code, zoo_animals.count(name)
gives the count of distinct elements in the list and not the index. You could do this instead.
for i in range(len(zoo_animals)):
print("The number {} in the list is {}".format(i 1,zoo_animals[i]))
Output
The number 1 in the list is dog
The number 2 in the list is cat
The number 3 in the list is fish
The number 4 in the list is shark
CodePudding user response:
zoo_animals = [ ]
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
zoo_animals.append(input("Enter an animal = "))
for name in zoo_animals:
print("The number ",zoo_animals.index(name) 1," in the list is ",name)