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 ...
随机推荐
- centos7配置阿里云yum源
1)下载repo文件 wget http://mirrors.aliyun.com/repo/Centos-7.repo 2)备份并替换系统的repo文件 cp Centos-7.repo /etc/ ...
- 城乡联谊胡策会糊厕R3
因为时间关系这把没设计题面,而且居然还出了锅……T_T 信 原题是leetcode WeeklyContest52 的T1(懒得去找url了 随便搞,但是无解输-1 数字统计 原题PE603 记前n个 ...
- Codeforces 834D The Bakery【dp+线段树维护+lazy】
D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...
- noi 2016 游记
先挖个坑..这回大概不会太监吧(大雾 day -2 下午起飞的飞机,晚上到了成都..把东西扔到旅馆后就组队外出觅食了... 街上人不多,逛了半天才发现一家卖本地小吃的小店. KPM:诶诶给我来碗酸辣粉 ...
- 并查集-HDU1232-畅通工程
转的其他人的.不知道谁的. 来看一个实例,杭电1232畅通工程 首先在地图上给你若干个城镇,这些城镇都可以看作点,然后告诉你哪些对城镇之间是有道路直接相连的.最后要解决的是整幅图的连通性问题.比如随意 ...
- poj_2503(map映射)
题目链接poj2503 Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 38820 Accepted: ...
- PL/SQL游标详解
刚打开游标的时候,是位于一个空行,要用fetch into 才能到第一行. 只是要注意用更新游标的时候,不能在游标期间commit. 否则会报ORA-01002: fetch out of seque ...
- c语言变量类型联想
int float char *(指针) 已经定义:单个变量 单个相同类型在内存中顺序存放:数组 不同单个类型在内存中顺序存放:结构体 不同类型在内存中自由存放:链表 其中结构体与链表类型需运用时提前 ...
- c++(排序二叉树)
前面我们讲过双向链表的数据结构.每一个循环节点有两个指针,一个指向前面一个节点,一个指向后继节点,这样所有的节点像一颗颗珍珠一样被一根线穿在了一起.然而今天我们讨论的数据结构却有一点不同,它有三个节点 ...
- maven学习3,如何创建一个web项目
Maven学习 (三) 使用m2eclipse创建web项目 1.首先确认你的eclipse已经安装好m2eclipse的环境,可以参照上两篇Maven学习内容 2.新建一个maven的项目 ...