Case sharing of list() list operator in Python
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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
