Python内置数据结构(一)

1、列表

(1)列表初始化

(2)下标/索引操作



(3)修改列表元素


(4)增加列表元素
1)append

2)insert


3)extend

(5)删除列表元素
1)pop


2)remove


3)clear

(6)查找/统计元素
1)index() 方法根据值找索引


2)count


(7)修改列表元素


(8)列表的其他方法
1)copy



2)函数原型实现

3)求2~100之间的素数
方法一:

其中:

方法二:

2、元组

(1)元组初始化

元组是不可变的,不可以用下标进行赋值操作。

(2)元组的操作

3、切片操作

lst=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]


lst[-40: 100]
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]

lst[:]
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]








4、封包/解包操作

(1)解包


(2)封包







5、字符串

Python 2 中的字符串是byte序列,Python 3 中的字符串是Unicode序列。
字符串是不可变的。
(1)字符串初始化


(2)字符串连接
1)join

2)“+”

(3)字符串分割
1)split


2)rsplit


3)splitlines

4)partition


5)rpartition

(4)字符串转换


(5)字符串位置调整
1)center

2)ljust

3)rjust

4)zfill

5)strip,lstrip,rstrip




(6)字符串判断
1)startswith


2)endswith


3)is_*


(7)查找替换
1)count

2)find


3)rfind

4)index,rindex

5)replace


6)in

6、格式化

(1)printf style
有两种形式:template % tupletemplate % dict







(2)format 方法







(3)jinja2框架

7、bytes和bytearray

(1)bytes
bytes是Python 3 特有的,Python 2 里不区分bytes和string。


str 使用encode方法转化为 bytes,bytes通过decode转化为str。

socket编程:

json编程:

(2)bytearray


发布了219 篇原创文章 · 获赞 603 · 访问量 129万+

猜你喜欢

转载自blog.csdn.net/gongxifacai_believe/article/details/94426870