适用于各种列表操作的Python程序

Here, we are implementing a python program for various list operations, following operations are being performed in the list,

在这里,我们正在为各种列表操作实现python程序,正在列表中执行以下操作,

  1. Declaring an integer list

    声明一个整数列表

  2. Printing the complete list

    打印完整列表

  3. Printing the first element of the list

    打印列表的第一个元素

  4. Printing ith element of the list

    打印列表的第i个元素

  5. Printing elements within the given indexes

    在给定索引中打印元素

  6. Printing a specific element using negative indexing

    使用负索引打印特定元素

  7. Appending an element to the list

    将元素追加到列表

  8. Finding the index of a specific element in the list

    在列表中查找特定元素的索引

  9. Sorting the list elements

    排序列表元素

  10. Popping an element from the list

    从列表中弹出一个元素

  11. Removing specified element from the list

    从列表中删除指定的元素

  12. Inserting an element at specified index

    在指定索引处插入元素

  13. Extending the list i.e. insert set of element (list) in the list

    扩展列表,即在列表中插入一组元素(列表)

  14. Reversing list elements

    反转列表元素

用于各种列表操作的Python代码 (Python code for various list operation)

# Python code for various list operation
# declaring a list of integers
iList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# List slicing operations
# printing the complete list
print('iList: ',iList)
# printing first element 
print('first element: ',iList[0]) 
# printing fourth element 
print('fourth element: ',iList[3]) 
# printing list elements from 0th index to 4th index
print('iList elements from 0 to 4 index:',iList[0: 5]) 
# printing list -7th or 3rd element from the list
print('3rd or -7th element:',iList[-7]) 
# appending an element to the list
iList.append(111)
print('iList after append():',iList)
# finding index of a specified element 
print('index of \'80\': ',iList.index(80))
# sorting the elements of iLIst
iList.sort()
print('after sorting: ', iList);
# popping an element
print('Popped elements is: ',iList.pop())
print('after pop(): ', iList);
# removing specified element
iList.remove(80)
print('after removing \'80\': ',iList)
# inserting an element at specified index 
# inserting 100 at 2nd index 
iList.insert(2, 100)
print('after insert: ', iList)
# counting occurances of a specified element
print('number of occurences of \'100\': ', iList.count(100))
# extending elements i.e. inserting a list to the list
iList.extend([11, 22, 33])
print('after extending:', iList)
#reversing the list
iList.reverse()
print('after reversing:', iList)

Output

输出量

iList:  [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]  
first element:  10
fourth element:  40  
iList elements from 0 to 4 index: [10, 20, 30, 40, 50]
3rd or -7th element: 40 
iList after append(): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 111] 
index of '80':  7 
after sorting:  [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 111] 
Popped elements is:  111
after pop():  [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]  
after removing '80':  [10, 20, 30, 40, 50, 60, 70, 90, 100] 
after insert:  [10, 20, 100, 30, 40, 50, 60, 70, 90, 100]
number of occurences of '100':  2
after extending: [10, 20, 100, 30, 40, 50, 60, 70, 90, 100, 11, 22, 33] 
after reversing: [33, 22, 11, 100, 90, 70, 60, 50, 40, 30, 100, 20, 10] 

翻译自: https://www.includehelp.com/python/program-for-various-list-operations.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544350.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一个障碍,就是一个超越自我的契机

一个障碍,就是一个新的已知条件,只要愿意,任何一个障碍,都会成为一个超越自我的契机。 有一天,素有森林之王之称的狮子,来到了 天神面前:"我很感谢你赐给我如此雄壮威武的体格、如此强大无…

JAVA基础之容器基础内容

Java Collections框架 Java Collections框架中包含了大量的集合接口以及这些接口的实现类和操作它们的方法,具体包含了Set(集合)、List(列表)、Map(键值对)、Queue(队列)、Stack(栈)等,其中List、Set、Queue、Stack都继承了Collection接口。…

更快的Maven构建工具mvnd和Gradle哪个性能更好?

作者 | 磊哥来源 | Java中文社群(ID:javacn666)转载请联系授权(微信ID:GG_Stone)Maven 作为经典的项目构建工具相信很多人已经用很久了,但如果体验过 Gradle,那感觉只有两个字“真香…

页面访问的常见错误码解析

200 OK 一切正常301 Moved Permanently 客户请求的文档在其他地方,新的URL在Location头中给出,浏览器应该自动地访问新的URL。 302 Found 类似于301,但新的URL应该被视为临时性的替代,而不是永久性的。注意,在HTT…

aptitude_PHP Numbers Aptitude问题与解答

aptitudeThis section contains Aptitude Questions and Answers on PHP Numbers. 本节包含有关PHP数字的能力问题。 1) PHP supports automatic type conversion? YesNo Answer & Explanation Correct answer: 1Yes Yes, PHP supports automatic type conversion. 1)PHP…

SpringBoot + ShardingSphere 秒级分库分表!

Spring Boot 作为主流微服务框架,拥有成熟的社区生态。市场应用广泛,为了方便大家,整理了一个基于spring boot的常用中间件快速集成入门系列手册,涉及RPC、缓存、消息队列、分库分表、注册中心、分布式配置等常用开源组件&#xf…

JAVA基础之自定义容器实现

容器 容器主要是指Collection所包含的实现类,常用的有List、Map以及Set三种结构。本文主要介绍了几种常见的集合实现类,对它们进行自定义实现。 ArrayList:有序的容器列表,顺序存储着元素,可以使用下标进行索引&…

git reset, git checkout, git revert 区别 (译)

博客原文地址: http://blog.mexiqq.com/index.php/archives/3/题记:团队中大多数成员使用 sourceTree 和 github 两款 git 工具,然而大家对于图形化工具提供的 reset,checkout,revert 功能点并不是很了解,甚至于混淆,然后凭借猜测去使用。功夫…

Redis笔记之基本数据结构 动态字符串SDS

简单动态字符串 传统上的C语言的字符串表示是以空字符结尾的字符数组(C字符串),redis自己实现一个动态字符串(SDS),两者之间的区别以及使用SDS的好处有: 结构不同。C字符串以空字符结尾的字符…

weakhashmap_Java WeakHashMap size()方法与示例

weakhashmapWeakHashMap类的size()方法 (WeakHashMap Class size() method) size() method is available in java.util package. size()方法在java.util包中可用。 size() method is used to get the number of key-value pairs that exist in this map. size()方法用于获取此映…

扯一把 Spring 的三种注入方式,到底哪种注入方式最佳?

1. 实例的注入方式首先来看看 Spring 中的实例该如何注入,总结起来,无非三种:属性注入set 方法注入构造方法注入我们分别来看下。1.1 属性注入属性注入是大家最为常见也是使用最多的一种注入方式了,代码如下:Service p…

在项目中引入领域驱动设计的经验

Chris Patuzzo近期在一次演讲中介绍了领域驱动设计(DDD)的原则,并结合一个基于Ruby on Rails的真实项目进行讲解。在这次项目之前,Chris所在的团队为重新设计公司的主营网站所做的两个概念验证都因为可伸缩性方面的问题而失败了。…

Redis笔记之基本数据结构 链表

链表 链表具有空间存储不连续,增删节点快的优点,因此redis在列表键、发布与订阅、慢查询、监视器等使用了链表作为底层实现。由于C语言中没有内置的链表实现,因此redis自己进行了实现。 双向链表。每个listtNode都有perv和next指针&#x…

treeset java_Java TreeSet iterator()方法与示例

treeset javaTreeSet类的iterator()方法 (TreeSet Class iterator() method) iterator() method is available in java.util package. iterator()方法在java.util包中可用。 iterator() method is used to iterate the elements of this TreeSet is ascending or increasing or…

SpringCloud组件:Ribbon负载均衡策略及执行原理!

大家好,我是磊哥。今天我们来看下微服务中非常重要的一个组件:Ribbon。它作为负载均衡器在分布式网络中扮演着非常重要的角色。本篇主要内容如下:在介绍 Ribbon 之前,不得不说下负载均衡这个比较偏僻的名词。为什么说它偏僻了&…

Redis笔记之基本数据结构 字典

字典 符号表、关联数组或者映射,有点类似于java中的map,用于保存键值对key-value。字典中的键key是独一无二的。底层实现为哈希表。下面进行简述: 哈希表。哈希表主要包含table数组、size、sizemask以及used。table用于保存哈希表节点&…

【零基础学习iOS开发】【02-C语言】02-第一个C语言程序

本文目录 前言一、编写第一个C语言程序-Hello World二、编译程序三、链接程序四、运行程序五、总结六、学习建议七、clang指令汇总回到顶部前言 前面已经唠叨了这么多理论知识,从这讲开始,就要通过接触代码来学习C语言的语法。学习任何一门语言&#xff…

安卓平板体验Java开发,还能白嫖一年阿里无影云,真香!(内含白嫖方法,人人可领)...

作者 | 磊哥来源 | Java中文社群(ID:javacn666)转载请联系授权(微信ID:GG_Stone)阿里无影云早有耳闻,前两天看朋友发体验照片,可能是程序员天生爱折腾的特性又发挥作用了&#xff0c…

Linux命令(三)

echo:输出信息echo ”abc”echo “字符串” |passwd --stdin USERNAME :利用管道修改用户密码输出重定向:>覆盖输出(会覆盖原文件中的内容)>>追加输出 (原有内容会被保留)Set –c &#xff1a…

strictmath_Java StrictMath scalb()方法与示例

strictmathStrictMath类scalb()方法 (StrictMath Class scalb() method) Syntax: 句法: public static double scalb(double do , int sf);public static float scalb(float fl , int sf);scalb(double do , int sf) method is used to return do* 2 raised to the…