search
HomeBackend DevelopmentPython TutorialCase sharing of list() list operator in Python

Case sharing of list() list operator in Python

Jul 24, 2017 pm 03:15 PM
listpythonCase

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment