2019年7月25日 地点:武汉
最近懒,一直没逼自己认真的去记录之前的知识点,一方面是量有点大,但主要的还是因为自己懒。从现在开始每天记录一点list方面的方法,争取早日写完。
关于list,列表类型的数据,我们用方括号表示,并且它不受方括号内部的影响,不像元祖这个数据类型,受其里面的数据类型改变而改变,详情请见01章。【注:本章节涉及list内容编写大部分使用random模块自动生成】
# 以下是list 的所有方法
print(dir(list))
"""
['__add__', '__class__', '__contains__', '__delattr__',
'__delitem__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__',
'__getitem__', '__gt__', '__hash__', '__iadd__',
'__imul__', '__init__', '__init_subclass__',
'__iter__', '__le__', '__len__', '__lt__',
'__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__reversed__',
'__rmul__', '__setattr__', '__setitem__',
'__sizeof__', '__str__', '__subclasshook__',
'append', 'clear', 'copy', 'count', 'extend',
'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
"""
目录
1、append
2、clear
3、copy
4、conut
5、extend
6、index
7、insert
8、pop
9、remove
10、reverse
11、sort
在list末尾增加新的对象。
def append(self, p_object): # real signature unknown; restored from __doc__
""" L.append(object) -> None -- append object to end """
pass
a = [1,2,3]
a.append(4)
print(a) # [1, 2, 3, 4]
清除list列表中的所有值
def clear(self): # real signature unknown; restored from __doc__
""" L.clear() -> None -- remove all items from L """
pass
a = [1,2,3,4]
print(a.clear())
复制列表,开辟另外的一块内存来储存内容。
def copy(self): # real signature unknown; restored from __doc__
""" L.copy() -> list -- a shallow copy of L """
return []
a = ["hello world!"]
b = a.copy()
print(a,id(a)) # ['hello world!'] 2283330152520
print(b,id(b)) # ['hello world!'] 2283329000136
count() 方法用于统计字符串里某个字符出现的次数。
def count(self, value): # real signature unknown; restored from __doc__
""" L.count(value) -> integer -- return number of occurrences of value """
return 0
import random
x = []
for i in range(20):
x.append(random.randrange(0,10))
print(x)
for i in range(10):
print("其中{}出现的次数为:{}".format(i,x.count(i)))
# [9, 4, 6, 1, 2, 5, 8, 8, 1, 7, 2, 8, 3, 9, 8, 0, 2, 2, 6, 3]
# 其中 0 出现的次数为:1
# 其中 1 出现的次数为:2
# 其中 2 出现的次数为:4
# 其中 3 出现的次数为:2
# 其中 4 出现的次数为:1
# 其中 5 出现的次数为:1
# 其中 6 出现的次数为:2
# 其中 7 出现的次数为:1
# 其中 8 出现的次数为:4
# 其中 9 出现的次数为:2
extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。
def extend(self, iterable): # real signature unknown; restored from __doc__
""" L.extend(iterable) -> None -- extend list by appending elements from the iterable """
pass
import random
x = []
y = []
for i in range(5):
x.append(random.randrange(0,10))
print("初始list为:",x) # 初始list为: [0, 0, 5, 6, 2]
for i in range(5):
y.append(random.randrange(0, 10))
print("应该添加的list为:",y) # 应该添加的list为: [0, 0, 6, 7, 9]
x.extend(y)
print("最后生成的list为:",x) # 最后生成的list为: [0, 0, 5, 6, 2, 0, 0, 6, 7, 9]
index() 函数用于从列表中找出某个值第一个匹配项的索引位置
【注:str类型中也有index的用法,包括sql,html语法中都有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
import random
x = [] # 创建一个空list,来存放数据
for i in range(10):
x.append(random.randrange(0,5))
print(x)
if (1 in x):
print(x.index(1))
index仅查看第一个查看元素所在的位置。
insert()用于指定对象将其插入指定位置。这里的tuple插在了第三个位置,可以理解成为外国人从0开始计数的,所以tuple在第3个位置。set同理。
def insert(self, index, p_object): # real signature unknown; restored from __doc__
""" L.insert(index, object) -- insert object before index """
pass
a = ['int','float','dict','list','str']
a.insert(2,'tuple')
a.insert(3,'set')
print('插入元素后列表为:',a)
# 插入元素后列表为: ['int', 'float', 'tuple', 'set', 'dict', 'list', 'str']
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
x = ['int', 'float', 'tuple', 'set', 'dict', 'list', 'str']
a = x.pop()
print(a,type(a)) # str
print(x) # ['int', 'float', 'tuple', 'set', 'dict', 'list']
x = [1,2,3,4,5]
a = x.pop()
print(a,type(a)) # 5
print(x) # [1, 2, 3, 4]
pop删除的元素的类型,和其储存在列表中的类型一样。
remove() 函数用于移除列表中某个值的第一个匹配项。pop默认从list最后端删除一个元素,remove从list前端删除指定元素。
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
x = ['int', 'float', 'tuple','float', 'set', 'dict','list','str']
x.remove('float')
print(x) # ['int', 'tuple', 'float', 'set', 'dict', 'list', 'str']
x.remove('float')
print(x) # ['int', 'tuple', 'set', 'dict', 'list', 'str']
reverse() 函数用于反向列表中元素。
def reverse(self): # real signature unknown; restored from __doc__
""" L.reverse() -- reverse *IN PLACE* """
pass
x = ['int', 'tuple', 'float', 'set', 'dict', 'list', 'str']
x.reverse()
print("反转后的list为:",x)
# 反转后的list为: ['str', 'list', 'dict', 'set', 'float', 'tuple', 'int']
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
a = ['str', 'list', 'dict', 'set', 'float', 'tuple', 'int']
a.sort()
print('升序输出',a) # 升序输出 ['dict', 'float', 'int', 'list', 'set', 'str', 'tuple']
# 按照字母的排序来排列list中的元素
a = ['str', 'list', 'dict', 'set', 'float', 'tuple', 'int']
a.sort(reverse=True)
print('降序输出:',a) # 降序输出: ['tuple', 'str', 'set', 'list', 'int', 'float', 'dict']