1 目录
- append
- clear
- copy
- count
- extend
- index
- insert
- pop
- remove
- reverse
- sort
2 说明
append
def append(self, p_object): # real signature unknown; restored from __doc__ """ L.append(object) -> None -- append object to end """ pass
clear
def clear(self): # real signature unknown; restored from __doc__ """ L.clear() -> None -- remove all items from L """ pass
copy
def copy(self): # real signature unknown; restored from __doc__ """ L.copy() -> list -- a shallow copy of L """ return []
count
def count(self, value): # real signature unknown; restored from __doc__ """ L.count(value) -> integer -- return number of occurrences of value """ return 0
extend
def extend(self, iterable): # real signature unknown; restored from __doc__ """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """ pass
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
insert
def insert(self, index, p_object): # real signature unknown; restored from __doc__ """ L.insert(index, object) -- insert object before index """ pass
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
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
reverse
def reverse(self): # real signature unknown; restored from __doc__ """ L.reverse() -- reverse *IN PLACE* """ pass
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()