Ad
How To Find The Index Of Two Similar Numbers In An Array?
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
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad