Python列表-一个打了激素的数组(3)

1、列表常用的操作符

● 比较操作符

 

  1.  >>> list1=[123]
  2.  >>> list2=[234]
  3.  >>> list2>list1
  4.  True
  5.  >>>

 

 

 

  1.  >>> list1=[123,234]
  2.  >>> list2=[234,345]
  3.  >>> list1>list2
  4.  False
  5.  >>>


当有多个元素默认是从第0个元素开始比较,也之比较第0个元素。

 

 

● 逻辑操作符

 

  1.  >>> list3=[123,234]
  2.  >>> (list1<list2) and (list1==list3)
  3.  True
  4.  >>>

 

 

● 连接操作符

 

  1.  >>> list4=list1+list2
  2.  >>> list4
  3.  [123, 234, 234, 345]
  4.  >>>

和extend()有相同之处。

 

 

 

  1.>>> list1+123
  2. Traceback (most recent call last):File "<pyshell#10>", line 1, in <module>
  3.   File "<pyshell#10>", line 1, in <module>
  4.     list1+123
  5.TypeError: can only concatenate list (not "int") to list
  6.>>>


直接在+后面添加元素是不可以的。 

 

 

● 重复操作符

 

  1.  >>> list3
  2.  [123, 234]
  3.  >>> list3*3
  4.  [123, 234, 123, 234, 123, 234]
  5.  >>>

 

 

 

  1.  >>> list3*=3
  2.  >>> list3
  3.  [123, 234, 123, 234, 123, 234]
  4.  >>>

改变了list3的值。 

 

● 成员关系操作符

in 、not 、in

 

  1.  >>> 123 in list3
  2.  True
  3.  >>> 'cisco' not in list3
  4.  True
  5.  >>> 'cisco' in list3
  6.  False
  7.  >>>

 

 

 

  1.  >>> list5 = [123,['cisco','lenovo'],235]
  2.  >>> 'cisco' in list5
  3.  False
  4.  >>>

cisco在列表中的列表,所以in、not in只能判断一个层次的成员关系。

 

 

  1.  >>> 'cisco' in list5[1]
  2.  True
  3.  >>>

在Python中访问列表中列表的元素的方法和java中的二维数组类似,使用两个中括号。

 

 

  1.  >>> list5[1][1]
  2.  'lenovo'
  3.  >>>

 

 

2、列表的小伙伴们

使用dir

 

  1.  >>> dir(list)
  2.  ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
  3.  >>>

 

 

● count() 

 

  1.  >>> list3.count(123)

count()方法,返回元素在列表中的出现次数。

 

● index()

  1.  >>> list3.index(123)
  2.  0
  3.  >>> list3.index(123,3,6)
  4.  4

返回元素在列表中的索引值,

 

list3.index(123,3,6)的意思是:在列表的索引从3到6的范围内返回元素123的索引值。

● reverse()

 

  1.  >>> list3.reverse()
  2.  >>> list3
  3.  [234, 123, 234, 123, 234, 123]

是将列表反转。

 

 

● sort()

 

  1.  >>> list3.sort()
  2.  >>> list3
  3.  [123, 123, 123, 234, 234, 234]
  4.  >>>

排序,默认的是从小到大排序。

 

如果从大到小的排序呢?

 

  1.  >>> list6=[1,4,2,3,9,6,8]
  2.  >>> list6.sort()
  3.  >>> list6
  4.  [1, 2, 3, 4, 6, 8, 9]

 

 

  1.  >>> list6.sort(reverse=True)
  2.  >>> list6
  3.  [9, 8, 6, 4, 3, 2, 1]
  4.  >>>

 

 

3、关于分片“拷贝”概念的补充

 

  1.  >>> list6
  2.  [9, 8, 6, 4, 3, 2, 1]
  3.  >>> list7=list6[:]
  4.  >>> list7
  5.  [9, 8, 6, 4, 3, 2, 1]
  6.  >>> list8=list6
  7.  >>> list6.sort()
  8.  >>> list6
  9.  [1, 2, 3, 4, 6, 8, 9]
  10.  >>> list7
  11.  [9, 8, 6, 4, 3, 2, 1]
  12.  >>> list8
  13.  [1, 2, 3, 4, 6, 8, 9]
  14.  >>>

使用等号(=)拷贝实际上只是将list8名贴到原来的内存存储地址上,并没有真正的拷贝。而分片的拷贝是在内存中重新分配一块存储空间来存储数据。
也就是说list8和list6实际上是指向内存中的同一个地址,而list7是指向了新的内存地址。 


 

注:列表推导式(List comprehensions)也叫列表解析,灵感取自函数式编程语言Haskell。是一个非常有用和灵活的工具,可以用来动态创建列表,语法:

[有关A的表达式 for A in B]

例如:

 

  1.  >>> [i*i for i in range(10)]
  2.  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

 

  1.  >>> list1 = [x**2 for x in range(10)]
  2.  >>> list1
  3.  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]



 



思考:

 

    1.  >>> name='2.cisco'
    2.  >>> name[:2]
    3.  '2.'
    4.  >>> name[2:]
    5.  'cisco'
    6.  >>> for i in name:
    7.  print(i)
    8.   
    9.   
    10.  2
    11.  .
    12.  c
    13.  i
    14.  s
    15.  c
    16.  o
    17.  >>> len(name)
    18.  7
    19.  >>>

转载:https://blog.csdn.net/alvinpanda/article/details/54427413

posted @ 2020-12-01 10:01  蔡地像徐坤  阅读(69)  评论(0编辑  收藏  举报