Ad

How To Find The Index Of Two Similar Numbers In An Array?

- 1 answer

Here is my program,

item_no = []
max_no = 0
for i in range(5):
    input_no = int(input("Enter an item number: "))
    item_no.append(input_no)
for i in item_no:
    if no > max_no:
       max_no = no
high = item_no.index(max_no)
print (item_no[high])

Example input: 5, 6, 7, 8, 8

Example output: 8

How can I change my program to output the same highest numbers in an array and how do I find the index of the result in (item_no)?

Expected output: 8, 8

Expected index for the result in item_no: 3, 4

Ad

Answer

I would use max() to find the maximum values.

item_no = []
for i in range(5):
    input_no = int(input("Enter an item number: "))
    item_no.append(input_no)

m = max(item_no)
max_values = [i for i in item_no if i == m]
max_values_indexes = [i for i, j in enumerate(item_no) if j == m]

print(max_values)
print(max_values_indexes)

Output using 5, 6, 7, 8, 8 as input:

[8, 8]
[3, 4]
Ad
source: stackoverflow.com
Ad