码迷,mamicode.com
首页 > 编程语言 > 详细

python内置数据结构

时间:2018-09-05 00:46:38      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:ble   pen   计数器   地址   res   with   tar   数据   pop   

Python内置数据结构

一、数值型

1.数据类型分类:

int:整数

python3的int就是长整型,且没有大小限制,受限于内存区域的大小
int(x) 返回一个整数

float:浮点数

有整数部分和小数部分组成。支持十进制和科学计数法表示。只有双精度型。
float(x) 返回一个浮点数

complex:复数

有实数和虚数部分组成,实数和虚数部分都是浮点数,3+4.2J
complex(x)、complex(x,y) 返回一个复数

bool:布尔

int的子类,仅有2个实例True、False对应1和0,可以和整数直接运算
bool(x) 返回布尔值,bool值判断逻辑一如前文所述,如:
bool(‘‘) --> False
bool(0) --> False

2.数字处理函数:

(1)int()、floor()、ceil()、round()、// 区别:

int():

builtin内置函数
官方文档描述:
class int(x=0)
class int(x, base=10)
Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, return x.int(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int(‘010‘, 0) is not legal, while int(‘010‘) is, as well as int(‘010‘, 8).

对于浮点数,截断小数至零
即取整数部分

floor():

math模板
向下取整

ceil():

math模板
向上取整

round():

四舍六入五取偶

//:

整除并floor()
floored quotient of x and y

(2)数字处理函数_2

min()

min(iterable, [, key, default])
min(arg1, arg2, args[, key])

max()

max(iterable, [, key, default])
max(arg1, arg2, args[, key])

pow(x,y)

功能与x**y相同

math.sqrt()

开方

bin()

返回值为字符串

oct()

返回值为字符串

hex()

返回值为字符串

math.pi

math.e

(3)类型判断

type(obj)

返回类型而不是字符串

isinstance(obj, class_or_tuple)

返回布尔值

二、序列对象

数据类型分类:

1.str:字符串

详见:下章
2.list:列表

列表内元素有顺序,可以使用索引
线性的数据结构
列表是可变的

初始化
例:

lst=list()
lst=[]
lst=[1,2,‘a‘,[‘i‘,‘j‘]]
lst=[range(5)]

不能一开始就定义大小,对比java

列表list、链表、queue、stack的差异

list线性存储,查询效率高O(1),插入、删除效率低O(n)
链表散落在内存中,查询效率低O(n),插入、删除效率高O(1)
queue先进先出FIFO
栈后进先出LIFO

列表索引访问

正索引:从左至右,从0开始,为列表中每一个元素编号
负索引:从右至左,从-1开始

列表查询

index(value,[start,[stop]])
返回第一个匹配项的索引
只能从左向右遍历
匹配不到返回ValueError异常
时间复杂度O(n),因需遍历列表

count(value)

返回列表中匹配value的次数
时间复杂度O(n),因需遍历列表

len()

时间复杂度O(1)
计数器在每次向list中插入、删除时执行计数
因此调用len()时只打出计数器数值,不执行遍历操作

列表增加、插入元素

append(object) -> None

在尾部追加,返回None
修改原有对象,不生成新对象
时间复杂度O(1)

insert(index, object) -> None

在指定索引插入元素,返回None
修改原有对象,不生成新对象
时间复杂度O(n),因为插入后可能会发生后续元素在内存中进行依次后移操作

若index超界不报错:
超越上界,尾部追加
超越下界,头部追加

extend(iteratable) -> None

将可迭代对象的元素追加进来,返回None
修改原有对象,不生成新对象

  • -> list

连接操作,将两个列表连接起来
产生新的对象,原列表不变
本质上调用的是add()方法

  • -> list

重复操作,将本列表元素重复n次,返回新的列表

注意:

x=[[1,2,3]]
y=x*3
y[0][1]=200
y

结果为:
[[1, 200, 3], [1, 200, 3], [1, 200, 3]]

y = [1]*5
y[0] = 6
y[1] = 7
print(y)

结果为:
[6, 7, 1, 1, 1]

可暂时理解为:
*复制,对于复杂对象,复制的是引用,并非数据,复制的三个元素实际指向的是同一个内存地址
简单对象不影响
列表删除元素

remove(value) -> None

从左至右查找第一个匹配value的值,移除该元素,返回None
修改原有对象,不生成新对象
时间复杂度O(n),因为插入后可能会发生后续元素在内存中进行依次后移操作(列表在内存中连续顺序存储)

pop([index]) -> item

不指定索引index,就从列表尾部弹出一个元素
指定索引index,就从索引处弹出一个元素,索引超界抛出IndexError错误
时间复杂度:
不指定索引为O(1)
指定索引为O(n),因为插入后可能会发生后续元素在内存中进行依次前移操作(列表在内存中连续顺序存储)

clear() -> None

清除列表所有元素,剩下一个空列表

列表其它操作

reverse() -> None

将列表元素反转,返回None
修改原有对象,不生成新对象

sort(key=None, reverse=False) -> None

对列表元素进行排序,默认升序
修改原有对象,不生成新对象
reverse为True,反转,降序
key一个函数,指定key如何排序

in

3.tuple:元组

详见:下章

三、键值对

数据类型分类:

set:集合

详见:下章

dict:字典

详见:下章

python内置数据结构

标签:ble   pen   计数器   地址   res   with   tar   数据   pop   

原文地址:http://blog.51cto.com/13954894/2170419

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!