Python中的list

2018-11-20 03:24:55来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

list_lst = [

  ('增',),

  ('删',),

  ('改',),

  ('查',),

  ('排序方法',),

  ('表达式',),

  ('练习',),

]

 

增  

  >>> lst = ['a', 'b', 'c']

  >>> lst.append('d')

  >>> lst

  ['a', 'b', 'c', 'd']

  >>> lst.extend([1, 2])

  >>> lst

  ['a', 'b', 'c', 'd', 1, 2]

  >>> lst = ['a', 'b', 'c']

  >>>lst.pop()

  'c'

  >>>lst

  ['a', 'b']

  >>>lst = ['a', 'b', 'c']

  >>>lst.pop(-2)

  'b'

  >>>lst

  ['a', 'c']

  >>> lst = ['a', 'b', 'c']

  >>> lst.pop(1)

  'b'

  >>> lst

  ['a', 'c']

  >>> lst.remove('c')

  >>> lst

  ['a']

  >>> lst.clear()

  >>> lst

  []

  >>> del lst

  >>> lst

  NameError: name 'lst' is not defined

  >>>lst = ['a', 'b', 'c']

  >>>del lst[1:]

  >>>lst

  ['a']

  >>> lst = ['a', 'b', 'c']

  >>> lst[1] = 'e'

  >>> lst

  ['a', 'e', 'c']

  >>> lst = ['a', 'b', 'c']

  >>> lst[1:] = 'e'

  >>> lst

  ['a', 'e']

  >>>lst = ['a', 'b', 'c']

  >>>for i in lst:

  ...  i

  'a'

  'b'

  'c'

排序

  >>> lst = [6, 18, 4, 7]

  >>> lst.sort()

  >>> lst

  [4, 6, 7, 18]

  >>> lst = [6, 18, 4, 7]

  >>> lst.sort(reverse=True)

  >>> lst

  [18, 7, 6, 4]

  >>> lst = [6, 18, 4, 7]

  >>> lst.reverse()

  >>> lst

  [7, 4, 18, 6]

表达式

  >>> [str(i) for i in range(2, 11)] + list('AJQK')

  ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'J', 'Q', 'K']

 

练习

  在不用集合的情况下,为列表去重

    >>> lst = [1, 1, 2, 3, 4, 4, 4, 5]

    >>> new_lst = []

    >>> for i in lst:

    ...    if i not in new_lst:

    ...      new_lst.append(i)

    ...

    >>> lst = new_lst

    >>> lst

    [1, 2, 3, 4, 5]

  

  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:第二十三天- 模块 re

下一篇:(项目)在线教育平台(五)