0

Sorry If this has already been asked but I can't seem to find the answer I'm looking for, they all have involved using tools we aren't allowed to use yet. The question is I have to use a for loop to go through a list and find the minimum value and then return the index of that value. That part I'm okay with, the issue I'm having is how do I get the program to return all the occurrences of that min value?

Restrictions I can't use anything like enumerate or other such functions, I'm allowed to use the min function but that's it and it has to be done within a for loop.

Any help would be appreciated, thanks!

n = [1, 2, 3, -50, -60, 0, 6, 9, -60, -60]
for i in n:
    min_val = []
    item = min(n)
    item_index = n.index(item)
    min_val.append(item_index)

print(min_val)

CyberStems
  • 326
  • 2
  • 15
  • Does this answer your question? [How to find all occurrences of an element in a list?](https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list) – Trenton McKinney Dec 18 '19 at 01:11

4 Answers4

0

I'm assuming this is python so you can use numpy

import numpy as np

n = np.array([1, 2, 3, -50, -60, 0, 6, 9, -60, -60])
searchKey = -60
item_index = np.where(n == searchKey)[0]
item_index => array([4, 8, 9])

if you don't know the minimum value ahead of time you can use a for-loop like this:

minval = n[0]
for i in n:
    if i < minval:
         minval = i

then just replace searchKey with minval above

disclaimer: n is of type np.array not list, not sure if this matters to you but if so I can post a less eloquent list solution that doesn't use enumerate.

CyberStems
  • 326
  • 2
  • 15
0

The following list Comprehension will do the work in one line

min_val_idxs = [ x for x in range(len(n)) if n[x] == min(n)]
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Isuruse
  • 36
  • 1
  • 3
0

Using a for-loop:

  • create an empty list, indices
    • this is done before / outside of the loop, otherwise the list is reset with each iteration.
  • create the loop with for i in range(len(x)):, which essentially iterates through a list of index locations [0, 1, 2, 3, ..., len(x)-1]
    • for i in n: just iterates through each value in your list
  • in the loop, add any i, where x[i] is a match to min(x), to the list
def get_indices(x: list) -> list:
    indices = list()
    min_val = min(x)
    for i in range(len(x)):
        if x[i] == min_val:
            indices.append(i)
    return indices


print(get_indices(n))

>>> [4, 8, 9]
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
0

Here is the solution to this:

n = [1, 2, 3, -50, -60, 0, 6, 9, -60, -60]
for i in n:
    min_count = n.count(min(n)) # getting a count of the number of minimum values
    min_value = min(n)
    min_index_list = []
    for j in range(min_count):
        min_index = n.index(min_value)
        min_index_list.append(min_index)
        n[min_index] += 1
    break

print(min(n) - 1, min_index_list)
Rahul P
  • 2,493
  • 2
  • 17
  • 31