python的组合数据类型及其内置方法说明
python中,数据结构是通过某种方式(例如对元素进行编号),组织在一起数据结构的集合.
python常用的组合数据类型有:序列类型,集合类型和映射类型
在序列类型中,又可以分为列表和元组,字符串也属于序列类型
在集合类型中,主要有集合类型
在映射类型中,主要有字典类型,字典是可变序列
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
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的组合数据类型及其内置方法说明的更多相关文章
- python学习day7 数据类型及内置方法补充
http://www.cnblogs.com/linhaifeng/articles/7133357.html#_label4 1.列表类型 用途:记录多个值(一般存放同属性的值) 定义方法 在[]内 ...
- python元组-字典-集合及其内置方法(下)
列表补充 补充方法 清空列表 clear # clear 清空列表 l = [1, 2, 3, 4, 4] print(l.clear()) # clear没有返回值(None) print(l) # ...
- python入门之数据类型及内置方法
目录 一.题记 二.整形int 2.1 用途 2.2 定义方式 2.3 常用方法 2.3.1 进制之间的转换 2.3.2 数据类型转换 3 类型总结 三.浮点型float 3.1 用途 3.2 定义方 ...
- python 入门基础4 --数据类型及内置方法
今日目录: 零.解压赋值+for循环 一. 可变/不可变和有序/无序 二.基本数据类型及内置方法 1.整型 int 2.浮点型float 3.字符串类型 4.列表类型 三.后期补充内容 零.解压赋值+ ...
- day6 基本数据类型及内置方法
day6 基本数据类型及内置方法 一.10进制转其他进制 1. 十进制转二进制 print(bin(11)) #0b1011 2. 十进制转八进制 print(hex(11)) #0o13 3. 十进 ...
- while + else 使用,while死循环与while的嵌套,for循环基本使用,range关键字,for的循环补充(break、continue、else) ,for循环的嵌套,基本数据类型及内置方法
今日内容 内容概要 while + else 使用 while死循环与while的嵌套 for循环基本使用 range关键字 for的循环补充(break.continue.else) for循环的嵌 ...
- Day 07 数据类型的内置方法[列表,元组,字典,集合]
数据类型的内置方法 一:列表类型[list] 1.用途:多个爱好,多个名字,多个装备等等 2.定义:[]内以逗号分隔多个元素,可以是任意类型的值 3.存在一个值/多个值:多个值 4.有序or无序:有序 ...
- if循环&数据类型的内置方法(上)
目录 if循环&数据类型的内置方法 for循环 range关键字 for+break for+continue for+else for循环的嵌套使用 数据类型的内置方法 if循环&数 ...
- wlile、 for循环和基本数据类型及内置方法
while + else 1.while与else连用 当while没有被关键字break主动结束的情况下 正常结束循环体代码之后执行else的子代码 """ while ...
随机推荐
- js面向对象学习笔记(一):创建空对象,理解this指向
var obj = new Object();//创建一个空对象 obj.name = '小王';//属性 obj.sayName = function () { //对象方法 对象最重要的是this ...
- 洛谷 P1177 【模板】快速排序【13种排序模版】
P1177 [模板]快速排序 题目描述 利用快速排序算法将读入的N个数从小到大排序后输出. 快速排序是信息学竞赛的必备算法之一.对于快速排序不是很了解的同学可以自行上网查询相关资料,掌握后独立完成.( ...
- Codeforces 810C Do you want a date?(数学,前缀和)
C. Do you want a date? time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- [bzoj1783] [Usaco2010 Jan]Taking Turns
题意: 一排数,两个人轮流取数,保证取的位置递增,每个人要使自己取的数的和尽量大,求两个人都在最优策略下取的和各是多少. 注:双方都知道对方也是按照最优策略取的... 傻逼推了半天dp......然后 ...
- BZOJ1008: [HNOI2008]越狱-快速幂+取模
1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 8689 Solved: 3748 Description 监狱有 ...
- C语言函数的作用域规则
“语言的作用域规则”是一组确定一部分代码是否“可见”或可访问另一部分代码和数据的规则. “同一函数中,不同的结构体成员名能相同,当变量处于不同的作用域时,名称可以相同. 注:作用域,其对象是变量, ...
- sql中查询同一列所有值出现的次数
尊重原创:http://blog.csdn.net/love_java_cc/article/details/52234889 有表如下table3: 需要查询country中各个国家出现的次数 SQ ...
- Python 3 利用 Dlib 19.7 实现人脸识别和剪切
0.引言 利用python开发,借助Dlib库进行人脸识别,然后将检测到的人脸剪切下来,依次排序显示在新的图像上: 实现的效果如下图所示,将图1原图中的6张人脸检测出来,然后剪切下来,在图像窗口中依次 ...
- 关于atom
以前老听别人说atom这款编辑器如何如何的好用,今天特地试了下,结果一不小心将顶部的工具栏给隐藏了,弄了半天都没弄出来.后来就在网上到处寻找帮助,试试这个试试那个,终于弄好了,其实是这样的. 首先在任 ...
- ES6中promise的使用方法
先看看ES5中异步编程的使用. let ajax = function (callBlack) { setTimeout(function () { callBlack && call ...