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的组合数据类型及其内置方法说明的更多相关文章

  1. python学习day7 数据类型及内置方法补充

    http://www.cnblogs.com/linhaifeng/articles/7133357.html#_label4 1.列表类型 用途:记录多个值(一般存放同属性的值) 定义方法 在[]内 ...

  2. python元组-字典-集合及其内置方法(下)

    列表补充 补充方法 清空列表 clear # clear 清空列表 l = [1, 2, 3, 4, 4] print(l.clear()) # clear没有返回值(None) print(l) # ...

  3. python入门之数据类型及内置方法

    目录 一.题记 二.整形int 2.1 用途 2.2 定义方式 2.3 常用方法 2.3.1 进制之间的转换 2.3.2 数据类型转换 3 类型总结 三.浮点型float 3.1 用途 3.2 定义方 ...

  4. python 入门基础4 --数据类型及内置方法

    今日目录: 零.解压赋值+for循环 一. 可变/不可变和有序/无序 二.基本数据类型及内置方法 1.整型 int 2.浮点型float 3.字符串类型 4.列表类型 三.后期补充内容 零.解压赋值+ ...

  5. day6 基本数据类型及内置方法

    day6 基本数据类型及内置方法 一.10进制转其他进制 1. 十进制转二进制 print(bin(11)) #0b1011 2. 十进制转八进制 print(hex(11)) #0o13 3. 十进 ...

  6. while + else 使用,while死循环与while的嵌套,for循环基本使用,range关键字,for的循环补充(break、continue、else) ,for循环的嵌套,基本数据类型及内置方法

    今日内容 内容概要 while + else 使用 while死循环与while的嵌套 for循环基本使用 range关键字 for的循环补充(break.continue.else) for循环的嵌 ...

  7. Day 07 数据类型的内置方法[列表,元组,字典,集合]

    数据类型的内置方法 一:列表类型[list] 1.用途:多个爱好,多个名字,多个装备等等 2.定义:[]内以逗号分隔多个元素,可以是任意类型的值 3.存在一个值/多个值:多个值 4.有序or无序:有序 ...

  8. if循环&数据类型的内置方法(上)

    目录 if循环&数据类型的内置方法 for循环 range关键字 for+break for+continue for+else for循环的嵌套使用 数据类型的内置方法 if循环&数 ...

  9. wlile、 for循环和基本数据类型及内置方法

    while + else 1.while与else连用 当while没有被关键字break主动结束的情况下 正常结束循环体代码之后执行else的子代码 """ while ...

随机推荐

  1. python写一个翻译的小脚本

    起因: 想着上学看不懂English的PDF感慨万分........ 然后就有了翻译的脚本. 截图: 代码: #-*- coding:'utf-8' -*- import requests impor ...

  2. cs231n spring 2017 lecture6 Training Neural Networks I 听课笔记

    1. 激活函数: 1)Sigmoid,σ(x)=1/(1+e-x).把输出压缩在(0,1)之间.几个问题:(a)x比较大或者比较小(比如10,-10),sigmoid的曲线很平缓,导数为0,在用链式法 ...

  3. js面向对象学习笔记(三):原型

    //原型:改写对象下面公用的方法或者属性,让公用的方法或者属性在内存中只存在一份(提高性能)//原型:prototype :要写在构造函数的下面var arr =[1,2,3,4,5];var arr ...

  4. BZOJ:4333: JSOI2012 智者的考验

    4333: JSOI2012 智者的考验 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 68  Solved: 18[Submit][Status][ ...

  5. Link-Cut-Trees

    填坑,填坑,填坑…… 开篇镇人品……下文的比喻仅供娱乐…… 为了迎接JSZX校内互测,我临时填坑学了LCT…… 怎么说呢……我也是懵懵懂懂地看了N篇博客,对着标程敲上一发代码,然后才慢慢理解.这里推荐 ...

  6. 【Java学习笔记之五】java数组详解

    数组 概念 同一种类型数据的集合.其实数组就是一个容器. 数组的好处 可以自动给数组中的元素从0开始编号,方便操作这些元素. 格式1: 元素类型[] 数组名 = new 元素类型[元素个数或数组长度] ...

  7. [51nod1532]带可选字符的多字符串匹配

    有一个文本串,它的长度为m (1 <= m <= 2000000),现在想找出其中所有的符合特定模式的子串位置. 符合特定模式是指,该子串的长度为n (1 <= n <= 50 ...

  8. A + B Problem II(大数加法)

    http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/Other ...

  9. 学习Spring必学的Java基础知识(2)----动态代理

    Spring AOP使用动态代理技术在运行期织入增强的代码,为了揭示Spring AOP底层的工作机理,有必要对涉及到的Java知识进行学习.Spring AOP使用了两种代理机制:一种是基于JDK的 ...

  10. SQL的case when then else end语句的用法

    SELECT a.managecom, a.subtype, count(*) loadsucc, sum(case when a.state in  ('4', '5', '6', '7', '8' ...