# Return the indexes of the smallest number in the list i.e. 0.5 number_list = [0.5, 1, 3, 10, 100, 2, 3, 0.5, 50] lowest_value = None index_list = [] index = 0 smallest_value = None while index < len(number_list): current_value = number_list[index] if (smallest_value == None): smallest_value = current_value # if current value is == smallest value # we have another index we should add if (current_value == smallest_value): index_list.append(index) # if the current value is less than the smallest value # we have a new smallest value. We need to clear the index # list (because the indexes there don't matter anymore) update # the smallest value, and add it's index to the list that we just # emptied elif (current_value < smallest_value): index_list = [index] # or index_list = [] then index_list.append(index) smallest_value = current_value # increment our index for the next loop index += 1 print(smallest_value) # 0.5 print(index_list) # you got it right if the screen shows [0, 7]
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question