教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 Python常用的组合数据类型汇总

Python常用的组合数据类型汇总

发布时间:2021-12-17   编辑:jiaochengji.com
教程集为您提供Python常用的组合数据类型汇总等资源,欢迎您收藏本站,我们将为您提供最新的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

<pre class="brush:php;toolbar:false">#在列表的末尾添加元素 L.append(object) -- append object to end</pre><pre class="brush:php;toolbar:false">>>> 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]</pre>

clear

<pre class="brush:php;toolbar:false">#清除列表中的所有元素 L.clear() -> None -- remove all items from L</pre><pre class="brush:php;toolbar:false">>>> l1=["hello","world"] >>> l2=[1,2,3,4] >>> l1.clear() >>> print(l1) [] >>> l2.clear() >>> print(l2) []</pre>

copy

<pre class="brush:php;toolbar:false">#浅复制 L.copy() -> list -- a shallow copy of L</pre><pre class="brush:php;toolbar:false">>>> l1=["hello","world","linux"] >>> id(l1) 140300326525832 >>> l2=l1.copy() >>> id(l2) 140300326526024 >>> print(l1) ['hello', 'world', 'linux'] >>> print(l2) ['hello', 'world', 'linux']</pre>

count

<pre class="brush:php;toolbar:false">#返回某个元素在列表中出现的次数 L.count(value) -> integer -- return number of occurrences of value</pre><pre class="brush:php;toolbar:false">>>> l1=[1,2,3,4,2,3,4,1,2] >>> l1.count(1) 2 >>> l1.count(2) 3 >>> l1.count(4) 2</pre>

extend

<pre class="brush:php;toolbar:false">#把另一个列表扩展进本列表中 L.extend(iterable) -- extend list by appending elements from the iterable</pre><pre class="brush:php;toolbar:false">>>> l1=["hello","world"] >>> l2=["linux","python"] >>> l1.extend(l2) >>> print(l1) ['hello', 'world', 'linux', 'python']</pre>

index

<pre class="brush:php;toolbar:false">#返回一个元素第一次出现在列表中的索引值,如果元素不存在报错 L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present.</pre><pre class="brush:php;toolbar:false">  >>> 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</pre>

相关推荐:《Python视频教程》

insert

<pre class="brush:php;toolbar:false">#在这个索引之前插入一个元素 L.insert(index, object) -- insert object before index</pre><pre class="brush:php;toolbar:false">    >>> 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']</pre>

pop

<pre class="brush:php;toolbar:false">#移除并返回一个索引上的元素,如果是一个空列表或者索引的值超出列表的长度则报错 L.pop([index]) -> item -- remove and return item at index (default last).Raises IndexError if list is empty or index is out of range.</pre><pre class="brush:php;toolbar:false">    >>> 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</pre>

remove

<pre class="brush:php;toolbar:false">#移除第一次出现的元素,如果元素不存在则报错 L.remove(value) -- remove first occurrence of value. Raises ValueError if the value is not present.</pre><pre class="brush:php;toolbar:false">    >>> 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</pre>

reverse

<pre class="brush:php;toolbar:false">#原地反转列表 L.reverse() -- reverse *IN PLACE*</pre><pre class="brush:php;toolbar:false">  >>> l1=['hello', 'world', 'linux', 'python']     >>> id(l1)     140300326525832     >>> l1.reverse()####     >>> print(l1)     ['python', 'linux', 'world', 'hello']     >>> id(l1)     140300326525832</pre>

sort

<pre class="brush:php;toolbar:false">#对列表进行原地排序 L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;</pre><pre class="brush:php;toolbar:false">    >>> 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</pre>

元组的常用内置方法

元组则使用()创建,例如('hello','world'),元组是不可变序列,其主要表现为元组的元素不可以修改,但是元组的元素的元素可以被修改。

元组属于序列类型,可以在python解释器中使用dir(tuple)查看元组的内置方法。

count

<pre class="brush:php;toolbar:false">#返回某个元素在元组中出现的次数 T.count(value) -> integer -- return number of occurrences of value</pre><pre class="brush:php;toolbar:false">>>> t1=("hello","world",1,2,3,"linux",1,2,3) >>> t1.count(1) 2 >>> t1.count(3) 2 >>> t1.count("hello") 1</pre>

index

<pre class="brush:php;toolbar:false">#返回元素在元组中出现的第一个索引的值,元素不存在则报错 T.index(value, [start, [stop]]) -> integer -- return first index of value.Raises ValueError if the value is not present.</pre><pre class="brush:php;toolbar:false">>>> 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</pre>

字典的常用内置方法

字典属于映射类型,可以在python解释器中使用dir(dict)查看字典的内置方法

clear

<pre class="brush:php;toolbar:false">#清除字典中所有的元素 D.clear() -> None.  Remove all items from D.</pre><pre class="brush:php;toolbar:false">>>> dic1={"k1":11,"k2":22,"k3":"hello","k4":"world"} >>> print(dic1) {'k3': 'hello', 'k4': 'world', 'k2': 22, 'k1': 11} >>> dic1.clear() >>> print(dic1) {}</pre>

copy

<pre class="brush:php;toolbar:false">#浅复制 D.copy() -> a shallow copy of D</pre><pre class="brush:php;toolbar:false">>>> 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}</pre>

fromkeys(iterable, value=None, /)

<pre class="brush:php;toolbar:false">#返回一个以迭代器中的每一个元素做健,值为None的字典 Returns a new dict with keys from iterable and values equal to value.</pre><pre class="brush:php;toolbar:false">>>> 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}</pre>

get

<pre class="brush:php;toolbar:false">#查询某个元素是否在字典中,即使不存在也不会报错 D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.</pre><pre class="brush:php;toolbar:false">>>> 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</pre>

items

<pre class="brush:php;toolbar:false">#返回一个由每个键及对应的值构成的元组组成的列表 D.items() -> a set-like object providing a view on D's items</pre><pre class="brush:php;toolbar:false">>>> 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'></pre>

keys

<pre class="brush:php;toolbar:false">#返回一个由字典所有的键构成的列表 D.keys() -> a set-like object providing a view on D's keys</pre><pre class="brush:php;toolbar:false">>>> dic1={"k1":11,"k2":22,"k3":"hello","k4":"world"} >>> dic1.keys() ['k3', 'k2', 'k1', 'k4']</pre>

pop

<pre class="brush:php;toolbar:false">#从字典中移除指定的键,返回这个键对应的值,如果键不存在则报错 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</pre><pre class="brush:php;toolbar:false">>>> dic1={"k1":11,"k2":22} >>> dic1.pop("k1") 11 >>> dic1.pop("k2") 22</pre>

popitem

<pre class="brush:php;toolbar:false">#从字典中移除一个键值对,并返回一个由所移除的键和值构成的元组,字典为空时,会报错 D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.</pre><pre class="brush:php;toolbar:false">>>> 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'</pre>

setdefault

<pre class="brush:php;toolbar:false">#参数只有一个时,字典会增加一个键值对,键为这个参数,值默认为None;后接两个参数时,第一个参数为字典新增的键,第二个参数为新增的键对应的值 D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D>>> dic1={"k1":11,"k2":"hello"}</pre><pre class="brush:php;toolbar:false">>>> 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'}</pre>

update

<pre class="brush:php;toolbar:false">#把一个字典参数合并入另一个字典,当两个字典的键有重复时,参数字典的键值会覆盖原始字典的键值 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]</pre><pre class="brush:php;toolbar:false">>>> 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'}</pre>

values

<pre class="brush:php;toolbar:false">#返回一个由字典的所有的值构成的列表 D.values() -> an object providing a view on D's values</pre><pre class="brush:php;toolbar:false">>>> dic1={"k1":11,"k2":22,"k3":"hello","k4":"world"} >>> dic1.values() ['hello', 22, 11, 'world']</pre>

您可能感兴趣的文章:
python中都有哪些数据类型
python序列中可变数据类型有什么
c 中const变量问题的简单分析
PostgreSQL从菜鸟到专家系列教程(1)PostgreSQL介绍
最常用的36个Pandas函数
python集合是否可变总结
python和c语言的区别是什么
jQuery数组处理方法汇总
python怎么区分不同数据类型
python是汇编语言吗

[关闭]
~ ~