Python中的list

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]

  

  

posted @ 2018-11-16 21:04  风掠丶幽兰  阅读(334)  评论(0编辑  收藏  举报