Home  >  Article  >  Backend Development  >  Case sharing of list() list operator in Python

Case sharing of list() list operator in Python

黄舟
黄舟Original
2017-07-24 15:15:082025browse

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