python list

1 目录

  1. append
  2. clear
  3. copy
  4. count
  5. extend
  6. index
  7. insert
  8. pop
  9. remove
  10. reverse
  11. sort

2 说明

  1. append

    def append(self, p_object): # real signature unknown; restored from __doc__
            """ L.append(object) -> None -- append object to end """
            pass
  2. clear

    def clear(self): # real signature unknown; restored from __doc__
            """ L.clear() -> None -- remove all items from L """
            pass
  3. copy

    def copy(self): # real signature unknown; restored from __doc__
            """ L.copy() -> list -- a shallow copy of L """
            return []
  4. count

     def count(self, value): # real signature unknown; restored from __doc__
            """ L.count(value) -> integer -- return number of occurrences of value """
            return 0
  5. extend

    def extend(self, iterable): # real signature unknown; restored from __doc__
            """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """
            pass
  6. index

     def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
            """
            L.index(value, [start, [stop]]) -> integer -- return first index of value.
            Raises ValueError if the value is not present.
            """
            return 0
  7. insert

    def insert(self, index, p_object): # real signature unknown; restored from __doc__
            """ L.insert(index, object) -- insert object before index """
            pass
  8. pop

     def pop(self, index=None): # real signature unknown; restored from __doc__
            """
            L.pop([index]) -> item -- remove and return item at index (default last).
            Raises IndexError if list is empty or index is out of range.
            """
            pass
  9. remove

    def remove(self, value): # real signature unknown; restored from __doc__
            """
            L.remove(value) -> None -- remove first occurrence of value.
            Raises ValueError if the value is not present.
            """
            pass
    
  10. reverse

    def reverse(self): # real signature unknown; restored from __doc__
            """ L.reverse() -- reverse *IN PLACE* """
            pass
  11. sort

      def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
            """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
            pass

3 例程

a_list = [1, 'nice', [2, 'good'], {3: 'perfect'}, (4, 'pretty')]
ad_1 = 'happy'
ad_2 = [5, 'glory']
ad_3 = {6: 'rejoice'}
ad_4 = (7, 'joyful')

print('a_list: ', a_list)
# a_list:  [1, 'nice', [2, 'good'], {3: 'perfect'}, (4, 'pretty')]

# 1. append
# 返回值: None
a_list.append(ad_1)
a_list.append(ad_2)
a_list.append(ad_3)
a_list.append(ad_4)
print('After append: a_list:', a_list)
# After append: a_list: [1, 'nice', [2, 'good'], {3: 'perfect'}, (4, 'pretty'), 'happy', [5, 'glory'], {6: 'rejoice'}, (7, 'joyful')]

# 4. count
# 返回值: integer
count_nice = a_list.count('nice')
count_perfect = a_list.count('perfect')
count_ad_1 = a_list.count(ad_1)
print('After append, count_nice: {}, count_perfect: {}, count_ad_1: {}.'.format(
    count_nice, count_perfect, count_ad_1
))
# After append, count_nice: 1, count_perfect: 0, count_ad_1: 1.

# 2. clear
# 返回值: None
a_list.clear()
print('After clear, a_list: {}'.format(a_list))
# After clear, a_list: []

a_list = [1, 'nice', [2, 'good'], {3: 'perfect'}, (4, 'pretty')]
# 3. copy
# 返回值: list
b_list = a_list.copy()
print('b_list: ', b_list)
# b_list:  [1, 'nice', [2, 'good'], {3: 'perfect'}, (4, 'pretty')]

# 5. extend
# 返回值: None
# 注意与 append 的区别
a_list.extend(ad_1)
a_list.extend(ad_2)
a_list.extend(ad_3)
a_list.extend(ad_4)
print('After extend: a_list:', a_list)
# After extend: a_list: [1, 'nice', [2, 'good'], {3: 'perfect'}, (4, 'pretty'), 'h', 'a', 'p', 'p', 'y', 5, 'glory', 6, 7, 'joyful']
# 注意,extend()字典时加进去键

# 4. count
# 返回值: integer
count_nice_1 = a_list.count('nice')
count_perfect_1 = a_list.count('perfect')
count_ad_1_1 = a_list.count(ad_1)
print('After extend, count_nice: {}, count_perfect: {}, count_ad_1:{}.'.format(
    count_nice_1, count_perfect_1, count_ad_1_1
))
# After extend, count_nice: 1, count_perfect: 0, count_ad_1:0.

# 6. index
# 返回值: integer
idx_nice = a_list.index('nice')
print('idx_nice: {}'.format(idx_nice))
try:
    idx_perfect = a_list.index('perfect')
except Exception as e:
    print(e)
# idx_nice: 1
# 'perfect' is not in list

# 7. insert
# 返回值: None
a_list.insert(4, 'ok')
print('After insert {} before index {}, a_list: {}'.format('ok', 4, a_list))
# After insert ok before index 4, a_list: [1, 'nice', [2, 'good'], {3: 'perfect'}, 'ok', (4, 'pretty'), 'h', 'a', 'p', 'p', 'y', 5, 'glory', 6, 7, 'joyful']

# 8. pop
# 返回值: item
item = a_list.pop()
print('After pop remove and return item at index (default last): {}, a_list: {}'.format(item, a_list))
# After pop remove and return item at index (default last): joyful, a_list: [1, 'nice', [2, 'good'], {3: 'perfect'}, 'ok', (4, 'pretty'), 'h', 'a', 'p', 'p', 'y', 5, 'glory', 6, 7]

# 9. remove
# 返回值: None
try:
    a_list.remove('nice')
    print('Remove nice successfully.')
    a_list.remove('nothing')
except Exception as e:
    print('nothing is not in a_list, failed remove it.')
    print(e)
# Remove nice successfully.
# nothing is not in a_list, failed remove it.
# list.remove(x): x not in list

# 10. reverse
# 返回值: None
a_list.reverse()
print('After reverse, a_list: {}'.format(a_list))
# After reverse, a_list: [7, 6, 'glory', 5, 'y', 'p', 'p', 'a', 'h', (4, 'pretty'), 'ok', {3: 'perfect'}, [2, 'good'], 1]

# 11. sort
# 返回值: None
a_list.sort()
print('After sort, a_list: {}'.format(a_list))
# Traceback (most recent call last):
#   File "<input>", line 1, in <module>
# TypeError: unorderable types: str() < int()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值