Python常用的组合数据类型汇总
python的组合数据类型及其内置方法说明
python中,数据结构是通过某种方式(例如对元素进行编号),组织在一起数据结构的集合。
python常用的组合数据类型有:序列类型,集合类型和映射类型。
(1)在序列类型中,又可以分为列表和元组,字符串也属于序列类型;
(2)在集合类型中,主要有集合类型;
(3)在映射类型中,主要有字典类型,字典是可变序列。
python中一切皆对象,组合数据类型也是对象,因此python的组合数据类型可以嵌套使用,列表中可以嵌套元组和字典,元组中也可以嵌套和字典,当然字典中也可以嵌套元组和列表,例如:['hello','world',[1,2,3]]。
元组,列表以及字符串等数据类型是"有大小的",也即其长度可使用内置函数len()测量。
python对象可以具有其可以被调用的特定“方法(函数)”。
列表的常用内置方法
在python中,列表使用[]创建,例如['hello','world','linux','python']。
列表是可变序列,其主要表现为:列表中的元素可以根据需要扩展和移除,而列表的内存映射地址不改变。
列表属于序列类型,可以在python解释器中使用dir(list)查看列表的内置方法。
append
#在列表的末尾添加元素L.append(object) -- append object to end
>>> l1=["hello","world"]>>> l2=[1,2,3,4]
>>> l1.append("linux")
>>> print(l1)
['hello', 'world', 'linux']
>>> l2.append(5)
>>> print(l2)
[1, 2, 3, 4, 5]
clear
#清除列表中的所有元素L.clear() -> None -- remove all items from L
>>> l1=["hello","world"]>>> l2=[1,2,3,4]
>>> l1.clear()
>>> print(l1)
[]
>>> l2.clear()
>>> print(l2)
[]
copy
#浅复制L.copy() -> list -- a shallow copy of L
>>> l1=["hello","world","linux"]>>> id(l1)
140300326525832
>>> l2=l1.copy()
>>> id(l2)
140300326526024
>>> print(l1)
['hello', 'world', 'linux']
>>> print(l2)
['hello', 'world', 'linux']
count
#返回某个元素在列表中出现的次数L.count(value) -> integer -- return number of occurrences of value
>>> l1=[1,2,3,4,2,3,4,1,2]>>> l1.count(1)
2
>>> l1.count(2)
3
>>> l1.count(4)
2
extend
#把另一个列表扩展进本列表中L.extend(iterable) -- extend list by appending elements from the iterable
>>> l1=["hello","world"]>>> l2=["linux","python"]
>>> l1.extend(l2)
>>> print(l1)
['hello', 'world', 'linux', 'python']
index
#返回一个元素第一次出现在列表中的索引值,如果元素不存在报错L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present.
>>> l1=[1,2,3,4,2,3,4,1,2]>>> l1.index(1)
0
>>> l1.index(2)
1
>>> l1.index(4)
3
>>> l1.index(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 5 is not in list
相关推荐:《Python相关教程》
insert
#在这个索引之前插入一个元素L.insert(index, object) -- insert object before index
>>> l1=['hello', 'world', 'linux', 'python']>>> l1.insert(1,"first")
>>> print(l1)
['hello', 'first', 'world', 'linux', 'python']
>>> l1.insert(1,"second")
>>> print(l1)
['hello', 'second', 'first', 'world', 'linux', 'python']
pop
#移除并返回一个索引上的元素,如果是一个空列表或者索引的值超出列表的长度则报错L.pop([index]) -> item -- remove and return item at index (default last).Raises IndexError if list is empty or index is out of range.
>>> l1=['hello', 'world', 'linux', 'python']>>> l1.pop()
'python'
>>> l1.pop(1)
'world'
>>> l1.pop(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range
remove
#移除第一次出现的元素,如果元素不存在则报错L.remove(value) -- remove first occurrence of value.
Raises ValueError if the value is not present.
>>> l1=['hello', 'world', 'linux', 'python']>>> l1.remove("hello")
>>> print(l1)
['world', 'linux', 'python']
>>> l1.remove("linux")
>>> print(l1)
['world', 'python']
>>> l1.remove("php")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
reverse
#原地反转列表L.reverse() -- reverse *IN PLACE*
>>> l1=['hello', 'world', 'linux', 'python']>>> id(l1)
140300326525832
>>> l1.reverse()####
>>> print(l1)
['python', 'linux', 'world', 'hello']
>>> id(l1)
140300326525832
sort
#对列表进行原地排序L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
>>> l1=[1,3,5,7,2,4,6,8]>>> id(l1)
140300326526024
>>> l1.sort()
>>> print(l1)
[1, 2, 3, 4, 5, 6, 7, 8]
>>> id(l1)
140300326526024
元组的常用内置方法
元组则使用()创建,例如('hello','world'),元组是不可变序列,其主要表现为元组的元素不可以修改,但是元组的元素的元素可以被修改。
元组属于序列类型,可以在python解释器中使用dir(tuple)查看元组的内置方法。
count
#返回某个元素在元组中出现的次数T.count(value) -> integer -- return number of occurrences of value
>>> t1=("hello","world",1,2,3,"linux",1,2,3)>>> t1.count(1)
2
>>> t1.count(3)
2
>>> t1.count("hello")
1
index
#返回元素在元组中出现的第一个索引的值,元素不存在则报错T.index(value, [start, [stop]]) -> integer -- return first index of value.Raises ValueError if the value is not present.
>>> t1=("hello","world",1,2,3,"linux")>>> t1=("hello","world",1,2,3,"linux",1,2,3)
>>> t1.count("hello")
1
>>> t1.index("linux")
5
>>> t1.index(3)
4
字典的常用内置方法
字典属于映射类型,可以在python解释器中使用dir(dict)查看字典的内置方法
clear
#清除字典中所有的元素D.clear() -> None. Remove all items from D.
>>> dic1={"k1":11,"k2":22,"k3":"hello","k4":"world"}>>> print(dic1)
{'k3': 'hello', 'k4': 'world', 'k2': 22, 'k1': 11}
>>> dic1.clear()
>>> print(dic1)
{}
copy
#浅复制D.copy() -> a shallow copy of D
>>> dic1={"k1":11,"k2":22,"k3":"hello","k4":"world"}>>> id(dic1)
140300455000584
>>> dic2=dic1.copy()
>>> id(dic2)####
140300455000648
>>> print(dic2)
{'k2': 22, 'k4': 'world', 'k3': 'hello', 'k1': 11}
fromkeys(iterable, value=None, /)
#返回一个以迭代器中的每一个元素做健,值为None的字典Returns a new dict with keys from iterable and values equal to value.
>>> dic1={"k1":11,"k2":"hello"}>>> dic1.fromkeys([22,33,44,55])
{33: None, 44: None, 22: None, 55: None}
>>> print(dic1)
{'k2': 'hello', 'k1': 11}
get
#查询某个元素是否在字典中,即使不存在也不会报错D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
>>> dic1={'k3': None, 'k2': 'hello', 'k1': 11, 'k4': 'world'}>>> dic1.get("k3")
>>> value1=dic1.get("k1")
>>> print(value1)
11
>>> value2=dic1.get("k2")
>>> print(value2)
hello
>>> value3=dic1.get("k5")
>>> print(value3)
None
items
#返回一个由每个键及对应的值构成的元组组成的列表D.items() -> a set-like object providing a view on D's items
>>> dic1={"k1":11,"k2":22,"k3":"hello","k4":"world"}>>> dic1.items()
dict_items([('k3', 'hello'), ('k4', 'world'), ('k2', 22), ('k1', 11)])
>>> type(dic1.items())
<class 'dict_items'>
keys
#返回一个由字典所有的键构成的列表D.keys() -> a set-like object providing a view on D's keys
>>> dic1={"k1":11,"k2":22,"k3":"hello","k4":"world"}>>> dic1.keys()
['k3', 'k2', 'k1', 'k4']
pop
#从字典中移除指定的键,返回这个键对应的值,如果键不存在则报错D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
>>> dic1={"k1":11,"k2":22}>>> dic1.pop("k1")
11
>>> dic1.pop("k2")
22
popitem
#从字典中移除一个键值对,并返回一个由所移除的键和值构成的元组,字典为空时,会报错D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.
>>> dic1={"k1":11,"k2":22}>>> dic1.popitem()
('k2', 22)
>>> dic1.popitem()
('k1', 11)
>>> dic1.popitem()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'popitem(): dictionary is empty'
setdefault
#参数只有一个时,字典会增加一个键值对,键为这个参数,值默认为None;后接两个参数时,第一个参数为字典新增的键,第二个参数为新增的键对应的值D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D>>> dic1={"k1":11,"k2":"hello"}
>>> dic1.setdefault("k3")>>> print(dic1)
{'k3': None, 'k2': 'hello', 'k1': 11}
>>> dic1.setdefault("k4","world")
'world'
>>> print(dic1)
{'k3': None, 'k2': 'hello', 'k1': 11, 'k4': 'world'}
update
#把一个字典参数合并入另一个字典,当两个字典的键有重复时,参数字典的键值会覆盖原始字典的键值D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]
>>> dic1={"k1":11,"k2":"hello"}>>> dic2={"k3":22,"k4":"world"}
>>> dic1.update(dic2)
>>> print(dic1)
{'k3': 22, 'k2': 'hello', 'k1': 11, 'k4': 'world'}
>>> dic1={"k1":11,"k2":"hello"}
>>> dic2={"k1":22,"k4":"world"}
>>> dic1.update(dic2)
>>> print(dic1)
{'k2': 'hello', 'k1': 22, 'k4': 'world'}
values
#返回一个由字典的所有的值构成的列表D.values() -> an object providing a view on D's values
>>> dic1={"k1":11,"k2":22,"k3":"hello","k4":"world"}>>> dic1.values()
['hello', 22, 11, 'world']
以上是 Python常用的组合数据类型汇总 的全部内容, 来源链接: utcz.com/z/521675.html