This article mainly introduces the Python list operator, and analyzes the usage skills of standard type operators, slicing, connection characters, list parsing, repeated operations, etc. in the form of examples. Friends in need can refer to it
The example in this article describes the Python list operator. Share it with everyone for your reference, the details are as follows:
#coding=utf8 ''''' 列表也可以使用比较操作符,比较时更加ASCII进行比较的。 比较列表时也用内建函数cmp()函数: 两个列表的元素分别比较,直到有一方胜出。 元组进行比较操作时和列表遵循相同的逻辑。 列表的切片操作和字符串的切片操作很像, 不过列表的切片操作返回的是一个对象或者几个对象的集合。 列表的切片操作也遵循从正负索引规则,也有开始索引值,结束索引值, 如果这两个值为空,默认为序列的开始和结束。 字符串类型只能用字符作为元素, 而列表类型的元素可以是任意类型的,如序列、字典、字符串、数字等。 可以在列表的元素上使用所有序列操作符或者在其之上执行序列类型内建的各种操作。 成员关系操作符(in,not in): 列表中可以检查一个对象是否是一个列表(或者元组)的成员。 成员关系操作运算符同样适用于元组类型。 连接操作符(+): 连接操作符允许把多个列表对象合并在一起。 列表类型的连接操作只能在同类型之间进行。 extend()函数也可以把一个列表的内容添加到另一个列表中去。 使用extend()方法比连接操作的一个优点是: 把新列表添加到了原有的列表里面,而不是像连接操作那样新建一个列表。 list.extend()方法也被用来做复合赋值运算。 连接操作符并不能实现向列表中添加新元素。 重复操作符(*): 重复操作符更多的应用在字符串类型中,不过, 列表和元组跟字符串同属序列类型,所以需要的时候也可以使用这一操作。 列表类型操作符和列表解析: python中没有专门用于列表类型的操作符。 列表可以使用大部分的对象和序列类型的操作符。 列表类型有属于自己的方法,列表才有的构建------列表解析。 列表解析是结合了列表的方括号和for循环,在逻辑上描述要创建的列表内容。 ''' #标准类型操作符:>,<,>=,<=,==,and,or,not,is,is not listOne=["ewang",789] listTwo=["hello",456] listThree_1=["hello"] listThree_2=["hello"] listThree=listThree_1 print "---------------------标准类型操作符-----------------------" #大于 if listTwo>listOne: print "listTwo>listOne" #大于等于 if listTwo>=listOne: print "listTwo>=listOne" #小于 if listOne<listTwo: print "listOne<listTwo" #小于等于 if listOne<=listTwo: print "listOne<=listTwo" #等于 if listThree_1==listThree_2: print "listThree_2==listThree_1" #不等于 if listOne != listTwo: print "listOne!=listTwo" #与:两个都为true结果为true if listTwo>listOne and listThree_1==listThree_2: print "listTwo>listOne and listThree_1==listThree_2" #或:两个位false结果为false if listTwo<=listOne or listThree_1==listThree_2: print "listTwo<=listOne and listThree_1==listThree_2" #非:取反操作 if not (listTwo<=listOne): print "not (listTwo<=listOne)" #不是同一个对象 if listThree_1 is not listThree_2: print " listThree_1 is not listThree_2" #同一个对象 if listThree_1 is listThree: print " listThree_1 is listThree" print "------------------------------------------------------------" print print "---------------------序列操作符-----------------------" print listOne[0:-1] print listOne[:-1] print listOne[0:] print listOne[1:2] print listOne[:] print listOne[1] listThree.append(listOne) print listThree[1][1] print listThree[1][:] print listThree[1][0:1] #对象是一个列表成员 if listOne in listThree: print listOne #对象不再列表中 if 888 not in listThree: print 888 #连接操作符+ mergerList=listOne+listTwo+listThree print mergerList #extend方法使用 listThree.extend(listOne) listThree.extend(listTwo) print listThree #重复操作符* print listOne*2 print listOne*3 print "--------------------------------------------------------" print print "---------------------列表解析-----------------------" numberList=[1,2,3,4,5,8,9,10,12,23.3,25.5] #所有元素乘上2 doubleNum=[num*2 for num in numberList] print doubleNum #跳出能被2整除的数 pTwo=[num for num in numberList if num%2==0] print pTwo print "------------------------------------------------------" print
Running results:
The above is the detailed content of Case sharing of list() list operator in Python. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
