index
is a list-searching function that performs a linear walk through the list to find the first position of a given element. This explains your confusing output--in the case of duplicates like 80, index()
will always give you the first index it can find for that element, which is 10.
Use enumerate()
if you're interested in obtaining the indices as a tuple for each element of the list.
Additionally, the variable i
suggests index, but actually represents a given temperature in the list; it's a misleading variable name.
temperatures = [33, 66, 65, 62, 59, 60, 62, 64, 70, 76, 80, 69, 80, 83, 68, 79, 61, 53, 50, 49, 53, 48, 45, 39]
hour_ex = []
for i, temperature in enumerate(temperatures):
if temperature > 70:
hour_ex.append(i)
print(hour_ex) # => [9, 10, 12, 13, 15]
Consider using a list comprehension, which performs a filtering operation on the enumerated list:
hour_ex = [i for i, temp in enumerate(temperatures) if temp > 70]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…