文章目录
- list
- tuple
- 返回数字
- 增加值
- 返回布尔值
- def __contains__(self, *args, **kwargs): # real signature unknown
- def __eq__(self, *args, **kwargs): # real signature unknown
- def __ge__(self, *args, **kwargs): # real signature unknown
- def __gt__(self, *args, **kwargs): # real signature unknown
- def __le__(self, *args, **kwargs): # real signature unknown
- def __lt__(self, *args, **kwargs): # real signature unknown
- def __ne__(self, *args, **kwargs): # real signature unknown
- 转换成哈希值
- 转换成可迭代对象
- 查
- range
- 通用
- 可变数据类型操作,也就是列表的操作
- 深浅copy
list
list类继承object,运行python程序时内置的数据类直接加载到内存,直接创建其实例化对象,可使用类中的方法。
- 创建列表
print([])
print([i for i in range(4)]) # list comprehension
print(list((9, 5, 3)))
运行结果
[]
[0, 1, 2, 3]
[9, 5, 3]
查
def getitem(self, y): # real signature unknown; restored from doc
""" x.__getitem__(y) <==> x[y] """
pass
def sizeof(self): # real signature unknown; restored from doc
""" L.__sizeof__() -- size of L in memory, in bytes """
pass
改
def setitem(self, *args, **kwargs): # real signature unknown
""" Set self[key] to value. """
pass
增
def append(self, p_object): doc
""" L.append(object) -> None -- append object to end """
pass
def insert(self, index, p_object):
""" L.insert(index, object) -- insert object before index """
pass
def extend(self, iterable):
""" L.extend(iterable) -> None -- extend list by appending elements from the iterable """
pass
def add(self, *args, **kwargs):
""" Return self+value. """
pass
def iadd(self, *args, **kwargs):
""" Implement self+=value. """
pass
def imul(self, *args, **kwargs):
""" Implement self*=value. """
pass
def mul(self, *args, **kwargs):
""" Return self*value.n """
pass
def rmul(self, *args, **kwargs):
""" Return self*value. """
pass
例:
l = ['a', 'b', 1]
l1 = ['a', 'b', 2]
l2 = ['a']
s = l + l1
print(s) # ['a', 'b', 1, 'a', 'b', 2]
j = l * 3
print(j) # ['a', 'b', 1, 'a', 'b', 1, 'a', 'b', 1]
s = l and l1
print(s) # ['a', 'b', 2]
s = l1 and l
print(s) # ['a', 'b', 1]
删
def clear(self):
""" L.clear() -> None -- remove all items from L """
pass
def pop(self, index=None):
"""
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
def remove(self, value):
"""
L.remove(value) -> None -- remove first occurrence of value.
Raises ValueError if the value is not present.
"""
pass
def delitem(self, *args, **kwargs): # real signature unknown
""" Delete self[key]. """
pass
复制
def copy(self):
""" L.copy() -> list -- a shallow copy of L """
return []
返回数字
def count(self, value):
""" L.count(value) -> integer -- return number of occurrences of value """
return 0
例
l = ['a', 'b', 1, 'a']
s = l.count('a') # 2
print(s)
def index(self, value, start=None, stop=None):
"""
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
def len(self, *args, **kwargs):
""" Return len(self). """
pass
def sizeof(self):
""" L.__sizeof__() -- size of L in memory, in bytes """
pass
例
l = ['a', 'b', 1, 'a']
s = l.__sizeof__() # 72
print(s)
返回可迭代对象
def iter(self, *args, **kwargs):
""" Implement iter(self). """
pass
例
l = ['a', 'b', 1, 'a']
s = iter(l)
print(s) # <list_iterator object at 0x000001B27BB1E2E8>
print(s.__sizeof__()) # 32
打乱顺序
def reverse(self):
""" L.reverse() -- reverse *IN PLACE* """
pass
def sort(self, key=None, reverse=False):
""" L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
pass
def reversed(self):
""" L.__reversed__() -- return a reverse iterator over the list """
pass
例
l = ['b', 'c', 'a']
l.reverse()
print(l)
l.sort()
print(l)
a = reversed(l)
print(list(a))
l = [9, 5, 4, 3, 8, 2, 6]
l.sort()
print(l)
l.sort(reverse=True) # 降序
print(l)
>>>
[2, 3, 4, 5, 6, 8, 9]
[9, 8, 6, 5, 4, 3, 2]
返回布尔值
def contains(self, *args, **kwargs):
""" Return key in self. """
pass
def eq(self, *args, **kwargs):
""" Return self==value. """
pass
def ge(self, *args, **kwargs):
""" Return self>=value. """
pass
def gt(self, *args, **kwargs):
""" Return self>value. """
pass
def le(self, *args, **kwargs):
""" Return self<=value. """
pass
def lt(self, *args, **kwargs):
""" Return self<value. """
pass
def ne(self, *args, **kwargs):
""" Return self!=value. """
pass
例
l = ['b', 'c', 'a', 'i']
l2 = ['b', 'c', 'a']
l3 = ['b', 'c', 'a']
print(l2 == l3) # True
print(l > l2) # True
print(l < l2) # False
print(l != l2) # True
转换为字符串
l = ['l', 'k']
s = ','.join(l)
print(s) # l,k
tuple
元组是不可变的序列,通常用于储存非寻常的数据类型,如由enumerate()这个内置函数生成的2元组。或者一些不能改变的数据存储,如集合或dict中的实例。元组也是一个类。可以通过以下方法建立:
print(())
print((4,))
print((4, 4, 3, 9))
print(tuple((9, 3, 5)))
print(tuple('abc/')) # 可以是一个序列
print(tuple([1, 2, 3])) # 可以是一个可迭代的容器
>>>
()
(4,)
(4, 4, 3, 9)
(9, 3, 5)
('a', 'b', 'c', '/')
(1, 2, 3)
元组可以使用上述通用的序列操作方法,可变的数据类型操作方法无法使用。
返回数字
def count(self, value): # real signature unknown; restored from doc
""" T.count(value) -> integer -- return number of occurrences of value """
return 0
def index(self, value, start=None, stop=None): # real signature unknown; restored from doc
"""
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
def len(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass
增加值
def add(self, *args, **kwargs): # real signature unknown
""" Return self+value. """
pass
def mul(self, *args, **kwargs): # real signature unknown
""" Return self*value.n """
pass
返回布尔值
def contains(self, *args, **kwargs): # real signature unknown
""" Return key in self. """
pass
def eq(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass
def ge(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass
def gt(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass
def le(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass
def lt(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass
def ne(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass
转换成哈希值
def hash(self, *args, **kwargs): # real signature unknown
""" Return hash(self). """
pass
转换成可迭代对象
def iter(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass
查
def getitem(self, *args, **kwargs): # real signature unknown
""" Return self[key]. """
pass
range
range类型的是不可变的数据类型。通常用于for循环。
range的参数一定要是整数。有__next__() 特殊方法的的对象,参数为整数。
例
r = range(0, 20, 2)
print(r)
print(10 in r)
print(r.index(10))
print(r[5])
print(r[:5])
print(r[-1])
print(range(0) == range(2, 1, 3))
print(range(0, 3, 2) == range(0, 4, 2)
>>>
>range(0, 20, 2)
True
5
10
range(0, 10, 2)
18
True
True
总结,list,tuple,range类型为python内置的序列类型,list为可变类型,tuple和range是不可变类型,可哈希
通用
- x in s
True if an item of s is equal to x,else False
print(3 in [1, 2, 3]) # True
print(4 in range(8)) #True
print('laura' in ('wendy', 'laura', 'tony')) #True
print(('laura', 'wendy') in ('laura', 'wendy', 'ilen')) #True
- x not in s
False if an item of s is equal to x,else True - s + t
the concatenation of s and t
print([9, 3, 4] + [5, 2, 9]) # [9, 3, 4, 5, 2, 9]
print(('laura', 'sandu') + ('ilen', 'iris')) # ('laura', 'sandu', 'ilen', 'iris')
s
浅copy:
>>>lists = [[]] * 3
>>>lists
[[], [], []]
>>>lists[0].append(3) # 这个列表里面的三个元素是引用的第一个元素的地址,不是复制值,类似浅copy
>>>lists
[[3], [3], [3]]
- s * n or n * s
equicalent to adding s to itself n times
print([8, 5] * 5)
print((1, 4, 6) * 3)
lists = [[]] * 3
运行结果
[8, 5, 8, 5, 8, 5, 8, 5, 8, 5]
(1, 4, 6, 1, 4, 6, 1, 4, 6)
[[], [], []]
- s[i]
item of s,origin()
print((3, 5, 2, 9)[2:])
print(lists)
运行结果如下
(2, 9)
- len(s)
- min()
- s.index(x[, i[, j]])
index of the first occurrence of x in s (at of after index i and before index j),raise error when x is not found - s.count(x)
total number of occurrences of x in s
可变数据类型操作,也就是列表的操作
- s[i] = x
item i of s is repleced by x - s[i:j] = t
slice of s from i to j is replaced by the contents of the iterable t - def s[i:j]
same as s[i:j] = [] - s[i:j:k] = t
the elements of s[i:j:k] are replaced by those of t, t must habe the same length as the slice it is replacing - del s[i:j:k]
removes the elements of s[i:j:k] from the list - s.append(x)
appends x to the end of the sequence (sanme as s[len(s):len(s)] = [x] - s.clear()
removes all items from s(same as del s[:]) - s.copy()
creates a shallow copy of s(same as s [:]
深浅copy
浅copy可以理解为懒copy,变量空间的每个值指向的是拷贝的每个值的内存地址。
例:
l1 = [1, ['ll', ], ('tt', ), {'aa': 'iuo'}, 98374958347]
l2 = l1.copy() # 相当于l1[:]
l3 = l1[:]
print(l1, id(l1), id(l1[0]), id(l1[1]), id(l1[2]), id(l1[3]), id(l1[4]))
print(l2, id(l2), id(l2[0]), id(l2[1]), id(l2[2]), id(l2[3]), id(l1[4]))
print(l3, id(l3), id(l3[0]), id(l3[1]), id(l3[2]), id(l3[3]), id(l1[4]))
运行结果
[1, ['ll'], ('tt',), {'aa': 'iuo'}, 98374958347] 2384716138632 1513319424 2384714313544 2384684046712 2384684368952 2384684485392
[1, ['ll'], ('tt',), {'aa': 'iuo'}, 98374958347] 2384716153480 1513319424 2384714313544 2384684046712 2384684368952 2384684485392
[1, ['ll'], ('tt',), {'aa': 'iuo'}, 98374958347] 2384716138376 1513319424 2384714313544 2384684046712 2384684368952 2384684485392
图示
- 深copy
不仅找到每个值的内存地址,还找到了真正的值, 再复制。相当于每个值的内存空间写入了真正的内容,而不是指向而已。(排除不可变数据类型:元组,数字,字符串,因为不可变数据类型只要占一个内存就行了,不能改变这个内存中的内容。除非删了)
import copy
l1 = [1, ['ll', 'kjhkjgg'], ('tt', 'iuiuiu', 654656548), {'aa': 'iuo'}, 345345354, 'ljghjhjhb']
l2 = copy.deepcopy(l1)
print(l1, id(l1), id(l1[0]), id(l1[1]), id(l1[2]), id(l1[3]), id(l1[4]), id(l1[5]))
print(l2, id(l2), id(l2[0]), id(l2[1]), id(l2[2]), id(l2[3]), id(l1[4]), id(l2[5]))
print(l1.pop())
print(l1)
print(l1, id(l1), id(l1[0]), id(l1[1]), id(l1[2]), id(l1[3]), id(l1[4]))
print(l2, id(l2), id(l2[0]), id(l2[1]), id(l2[2]), id(l2[3]), id(l1[4]), id(l2[5]))
运行结果
[1, ['ll', 'kjhkjgg'], ('tt', 'iuiuiu', 654656548), {'aa': 'iuo'}, 345345354, 'ljghjhjhb'] 2639253455240 1513319424 2639253455496 2639253413176 2639242642488 2639241916112 2639253440368
[1, ['ll', 'kjhkjgg'], ('tt', 'iuiuiu', 654656548), {'aa': 'iuo'}, 345345354, 'ljghjhjhb'] 2639253473736 1513319424 2639253472712 2639253413176 2639253265720 2639241916112 2639253440368
ljghjhjhb
[1, ['ll', 'kjhkjgg'], ('tt', 'iuiuiu', 654656548), {'aa': 'iuo'}, 345345354]
[1, ['ll', 'kjhkjgg'], ('tt', 'iuiuiu', 654656548), {'aa': 'iuo'}, 345345354] 2639253455240 1513319424 2639253455496 2639253413176 2639242642488 2639241916112
[1, ['ll', 'kjhkjgg'], ('tt', 'iuiuiu', 654656548), {'aa': 'iuo'}, 345345354, 'ljghjhjhb'] 2639253473736 1513319424 2639253472712 2639253413176 2639253265720 2639241916112 2639253440368
- 深浅copy对比
import copy
l1 = ['indkfh', 9898, ['dff', 98, [54, ['di']]]]
l2 = l1.copy()
l3 = copy.deepcopy(l2)
print(id(l1[2]), id(l1[2][2]), id(l1[2][2][1]))
print(id(l2[2]), id(l2[2][2]), id(l2[2][2][1]))
print(id(l3[2]), id(l3[2][2]), id(l3[2][2][1]))
import copy
n1 = {'d1': 'a', 'd2': 98999, 'd3': ['aaa', 455, [8984, ]]}
n2 = n1.copy()
n3 = copy.deepcopy(n2)
print(id(n1['d1']), id(n1['d2']), id(n1['d3']))
print(id(n2['d1']), id(n2['d2']), id(n2['d3']))
print(id(n3['d1']), id(n3['d2']), id(n3['d3']))
print(id(n1['d3'][2]))
print(id(n2['d3'][2]))
print(id(n3['d3'][2]))
运行结果
1762661370824 1762661351752 1762661336968
1762661370824 1762661351752 1762661336968
1762661384328 1762661384392 1762661384456
1762658496384 1762658781968 1762661384648
1762658496384 1762658781968 1762661384648
1762658496384 1762658781968 1762661384520
1762661384584
1762661384584
1762661384712