How to return the position of numbers, which are same as input numbers, in a list in python?
- 阅读更多 关于 How to return the position of numbers, which are same as input numbers, in a list in python?
问题 I have a list such as: [1,2,3,2,1,5,6] I want to find the all the positions of 1 in the list. I tried if statement, but I only get the position of first 1, not all 1s. The real output if I use if statement looks like [0] , but the expected result should be [0,4] . 回答1: You can use a list comprehension iterating over enumerate(your_list) and using an if statement as part of it to catch only the wanted values, as below: data = [1,2,3,2,1,5,6] # Your list num = 1 # The value you want to find