Given a list and we have to find the index/position of minimum and maximum elements of a list in Python.

给定一个列表,我们必须在Python中找到列表的最小和最大元素的索引/位置。

Prerequisite:

先决条件:

  • Python | min() Method

    Python | min()方法

  • Python | max() Method

    Python | max()方法

Example:

例:

    Input:
list = [10, 1, 2, 20, 3, 20]
Output:
Positive of minimum element:  1
Positive of maximum element:  3

Logic:

逻辑:

To find the positions/indexes of minimum and maximum elements of a list, we need to find the maximum and minimum elements of the list – to find the maximum element of the list, we will use max(list) and to find the minimum element of the list, we will use min(list).

找到列表的最小和最大元素的位置/索引 ,我们需要找到列表的最大和最小元素–要找到列表的最大元素,我们将使用max(list)并找到最小的元素在列表中,我们将使用min(list) 。

And, to get their indexes, we will use list.index(max(list)) and list.index(min(list)).

并且,要获取其索引,我们将使用list.index(max(list))和list.index(min(list)) 。

Program to find the position of min and max elements of a list in Python

程序以查找Python中列表的最小和最大元素的位置

# declare a list of Integers
list = [10, 1, 2, 20, 3, 20]
# min element's position/index
min = list.index (min(list))
# max element's position/index
max = list.index (max(list))
# printing the position/index of min and max elements
print "position of minimum element: ", min
print "position of maximum element: ", max

Output

输出量

    position of minimum element:  1position of maximum element:  3

Explanation:

说明:

  • The minimum number of the list is 1 and it is at 1st position in the list. To get it’s index, we use list.index(min(list)) statement, min(list) returns 1 (as minimum element) and list.index(1) returns the index of 1 from the list. Hence, the position of minimum element is: 1

    列表的最小数目是1,它是在列表1 位置。 为了得到它的索引,我们使用list.index(min(list))语句,min(list)返回1(作为最小元素), list.index(1)返回列表中的索引1。 因此,最小元素的位置为:1

  • The maximum number of the list if 20 and it is two times in the list, first occurrence of 20 is at 3rd position and the second occurrence of 20 is at 5th position. Statement max(list) returns the maximum element of the list, which is 20 and the statement list.index(20) returns the index/position of first matched element. Hence, the position of maximum element is: 3

    列表的最大数目,如果20,它是在该列表两次,20的第一次出现是在第三位置和20的第二次出现在 5位置。 语句max(list)返回列表的最大元素,即20,语句list.index(20)返回第一个匹配元素的索引/位置 。 因此,最大元素的位置是:3

翻译自: https://www.includehelp.com/python/find-the-position-of-minimum-and-maximum-elements-of-a-list.aspx

Python | 程序查找列表中最小和最大元素的位置相关推荐

  1. Python | 程序从列表中删除重复的元素

    Example: 例: Input: list1: [10, 20, 10, 20, 30, 40, 30, 50] Output: List after removing duplicate ele ...

  2. Python快速找到列表中所有重复的元素

    Python快速找到列表中所有重复的元素:https://blog.csdn.net/sinat_29957455/article/details/103886088 index方法 为了能够找到元素 ...

  3. python字符串查找重复项,Python程序查找字符串中所有重复的字符

    在本教程中,我们将学习如何在字符串中查找所有重复值.我们可以在Python中以不同的方式进行操作.让我们一一探讨. 我们要编写的程序的目的是查找字符串中存在的重复字符.例如,我们有一个字符串tutor ...

  4. Python | 程序从列表中删除范围内的所有元素

    Given a list and we have to remove elements in a range from the list in Python. 给定一个列表,我们必须从Python中的 ...

  5. python中怎么表示正数_在Python程序的列表中计算正数和负数

    在本文中,我们将学习下面给出的问题陈述的解决方案. 问题陈述-我们得到了一个可迭代的列表,我们需要计算其中的正数和负数并显示它们. 方法1:使用迭代构造的蛮力方法(for) 在这里,我们需要使用for ...

  6. python快速找到列表中出现最多的元素

    先上代码: #导入python内置库 from collections import Counter #定义一个名为n的列表 n = ['1','2','2','2','3','3','4','5'] ...

  7. python 找出列表中出现最多的元素_利用Python找出序列中出现最多的元素示例代码...

    前言 Python包含6种内置的序列:列表.元组.字符串 .Unicode字符串.buffer对象.xrange对象.在序列中的每个元素都有自己的编号.列表与元组的区别在于,列表是可以修改,而组元不可 ...

  8. Python:在列表中查找

    本文翻译自:Python: Find in list I have come across this: 我遇到了这个: item = someSortOfSelection() if item in ...

  9. Python查找列表中相加等于s的n个数字(combinations的使用)

    from random import randrange from itertools import combinations print("查找列表中相加等于s的n个数字: ") ...

  10. python找出第二大值,Python程序在Dictionary中查找第二个最大值

    在本文中,我们将学习下面给出的问题陈述的解决方案. 问题陈述-我们给了两个整数,我们需要在字典中打印第二个最大值 现在让我们观察一下下面的实现中的概念- 方法1:sorted()通过负索引使用函数 示 ...

最新文章

  1. Ios生产证书申请(含推送证书)
  2. AI杀入斗地主领域,快手开发DouZero对标AlphaZero,干掉344个AI获第一
  3. 吐槽: 移动端缓存策略
  4. TCC事务补偿机制实现分布式事务控制介绍
  5. SpringBoot 处理内置对象
  6. 深度好文:Linux操作系统内存
  7. 01-spring配置详解
  8. 训练Tesseract
  9. c++ 退出函数_UCOSIII源码分析之——bsp_os.c文件分析
  10. 7-6 What is a computer? (5 分)
  11. 点线面的特点_黑白装饰画——点线面 设计入门必备
  12. 洛谷 2017.7月赛解题报告
  13. MQTT测试工具MQTT.FX
  14. 图灵 计算机 ppt,人工智能导论(ppt 155页)
  15. 定时备份网站数据文件到阿里网盘
  16. 求到达必败态的方法数 ZOJ 3067 Nim
  17. Dropping Balls
  18. Android 9.0 简单适配
  19. Leetcode 1217. Minimum Cost to Move Chips to The Same Position [Python]
  20. can通讯bdc_纯电动汽车网络总线 数据通信网络连接系统介绍

热门文章

  1. linux系统开发安卓应用,在 Linux 里搭建 Android App 开发环境
  2. Brain Predicted Age (一)
  3. windows下qt的环境配置
  4. ppt图片特效 c语言实现,用了这么久的PPT,才知道PPT可以一键生成特效图片!太好看了...
  5. 为知笔记的快捷键整理
  6. 从 0 开始学游戏开发
  7. 搭建 WordPress 博客教程
  8. linux创建sudo用户组,如何将用户添加到sudo组
  9. 基于JavaEye-API实现的Gerry-聊天Dos版v1.0
  10. 在iOS7中修改键盘Return键的类型