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]

  

  

转载于:https://www.cnblogs.com/HopenZhang/p/9971616.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值