Python中常见的数据结构可以统称为容器(container)。序列(如列表和元组)、映射(如字典)以及集合(set)是三类主要的容器。

一、序列(列表、元组和字符串)

序列中的每个元素都有自己的编号。Python中有6种内建的序列。其中列表和元组是最常见的类型。其他包括字符串、Unicode字符串、buffer对象和xrange对象。下面重点介绍下列表、元组和字符串。

1、通用序列操作:

从列表、元组以及字符串可以“抽象”出序列的一些公共通用方法(不是你想像中的CRUD),这些操作包括:索引(indexing)、分片 (sliceing)、加(adding)、乘(multiplying)以及检查某个元素是否属于序列的成员。除此之外,还有计算序列长度、最大最小元 素等内置函数

1)索引:

 str1='Hello'
nums=[1,2,3,4]
t1=(123,234,345)
print str1[0]
print nums[1]
print t1[2]

输出:

H
2
345

2)分片:

分片操作用来访问一定范围内的元素。分片通过冒号相隔的两个索引来实现:

 nums=range(10)
print nums
print nums[1:5]
print nums[6:10]
print nums[1:]
print nums[-3:-1]
print nums[-3:] #包括序列结尾的元素,置空最后一个索引
print nums[:] #复制整个序列

输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4]
[6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[7, 8]
[7, 8, 9]

不同的步长,有不同的输出:

 nums=range(10)
print nums
print nums[0:10] #默认步长为1 等价于nums[1:5:1]
print nums[0:10:2] #步长为2
print nums[0:10:3] #步长为3
##print nums[0:10:0] #步长为0
print nums[0:10:-2] #步长为-2

输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 4, 6, 8]
[0, 3, 6, 9]
[]

3)序列相加:

两种相同类型的序列才可以相加;

 str1='Hello'
str2=' world'
print str1+str2
num1=[1,2,3]
num2=[2,3,4]
print num1+num2
print str1+num1

输出:

Hello world
[1, 2, 3, 2, 3, 4]

Traceback (most recent call last):
File “F:\Python\test.py”, line 7, in <module>
print str1+num1
TypeError: cannot concatenate ‘str’ and ‘list’ objects

4)乘法:

 print [None]*10
str1='Hello'
print str1*2
num1=[1,2]
print num1*2
print str1*num1

输出:

[None, None, None, None, None, None, None, None, None, None]

HelloHello
[1, 2, 1, 2]

Traceback (most recent call last):
File “F:\Python\test.py”, line 5, in <module>
print str1*num1
TypeError: can’t multiply sequence by non-int of type ‘list’

5)成员资格:

in运算符会用来检查一个对象是否为某个序列(或者其他类型)的成员(即元素):

 str1='Hello'
print 'h' in str1
print 'H' in str1
num1=[1,2]
print 1 in num1

输出:

False
True
True

6)长度、最小值和最大值:

 str1='Hello'
print len(str1)
print max(str1)
print min(str1)
num1=[1,2,1,4,123]
print len(num1)
print max(num1)
print min(num1)

输出:

5
o
H
5
123
1

2、列表

列表是可变的,这是它区别于字符串和元组的最重要的特点,一句话概括即:列表可以修改,而字符串和元组不能。

 list1=['hello','world']
print list1
list2=[1,2,3]
print list2

输出:

[‘hello’, ‘world’]
[1, 2, 3]

1)list函数:

通过list函数(其实list是一种类型而不是函数)对字符串创建列表非常有效:

 list3=list("hello")
print list3

输出:

[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

join函数:

实现将由字符组成的列表转换为字符串;

 somelist = ['a','b','c']
list1 = ``.join(somelist)
print list

输出:

'abc'

2)基本列表操作:

元素赋值:

 x = [1,1,1]
x[1] = 2
print x

输出:

[1,2,1]

删除元素:

 list1 = ['h','e','l','l','o']
del list1[1]
print list1

输出:

['h','l','l','o']

分片赋值:

 name = list('Perl')
name[1:] = list('ython')
print name

输出:

['P','y','t','h','o','n']

3)列表方法:

append方法:、

在列表末尾追加新的对象;

 list1 = [1,2,3]
list1.append(5)
print list1

输出:

[1,2,3,5]

count方法:

统计某个元素在列表中出现的次数;

result = ['to','be','or','not','to','be']
print result.count('to')

输出:

2

extend方法:

extend方法会改变原来的列表,连接操作(‘+’)不会改变原来的列表;

python数据结构详解的更多相关文章

  1. Python中的高级数据结构详解

    这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...

  2. Python Collections详解

    Python Collections详解 collections模块在内置数据结构(list.tuple.dict.set)的基础上,提供了几个额外的数据结构:ChainMap.Counter.deq ...

  3. redis 五种数据结构详解(string,list,set,zset,hash)

    redis 五种数据结构详解(string,list,set,zset,hash) Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存 ...

  4. Python闭包详解

    Python闭包详解 1 快速预览 以下是一段简单的闭包代码示例: def foo(): m=3 n=5 def bar(): a=4 return m+n+a return bar >> ...

  5. [转] Python Traceback详解

    追莫名其妙的bugs利器-mark- 转自:https://www.jianshu.com/p/a8cb5375171a   Python Traceback详解   刚接触Python的时候,简单的 ...

  6. [转]Redis内部数据结构详解-sds

    本文是<Redis内部数据结构详解>系列的第二篇,讲述Redis中使用最多的一个基础数据结构:sds. 不管在哪门编程语言当中,字符串都几乎是使用最多的数据结构.sds正是在Redis中被 ...

  7. python 数据类型详解

    python数据类型详解 参考网址:http://www.cnblogs.com/linjiqin/p/3608541.html 目录1.字符串2.布尔类型3.整数4.浮点数5.数字6.列表7.元组8 ...

  8. Python 递归函数 详解

    Python 递归函数 详解   在函数内调用当前函数本身的函数就是递归函数   下面是一个递归函数的实例: 第一次接触递归函数的人,都会被它调用本身而搞得晕头转向,而且看上面的函数调用,得到的结果会 ...

  9. redis 五种数据结构详解(string,list,set,zset,hash),各种问题综合

    redis 五种数据结构详解(string,list,set,zset,hash) https://www.cnblogs.com/sdgf/p/6244937.html redis 与 spring ...

随机推荐

  1. ssh远程端口转发

    当ssh的连接方向和应用连接的方向不一致时,这就称为ssh远程转发. 主机3是一台web server 应用请求是主机2到主机1 ssh请求是主机1到主机2 主机2开启ssh服务 service ss ...

  2. Log4j2配置及使用

    Log4j2:一个日志管理工具.Log4j的升级版,需要Java6以上   一.安装log4j2依赖包 1.通过maven的pom.xml直接引入jar: log4j-api和log4j-core & ...

  3. contextlib 上下文管理器

    在Python中,读写文件这样的资源要特别注意,必须在使用完毕后正确关闭它们.正确关闭文件资源的一个方法是使用try...finally: try: f = open('/path/to/file', ...

  4. Gym - 100989G 二分

    链接:ECJTU 2018 Summer Training 1 - Virtual Judge  https://vjudge.net/contest/236677#problem/G 谷歌翻译: 距 ...

  5. Unity3d插件Master Audio AAA Sound v3.5

    Unity3d声音类插件Master Audio AAA Sound v3.5.8.3Master Audio gives you tremendous ease of use, speed, pow ...

  6. ZOJ 1610 Count the Color(线段树区间更新)

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

  7. Xcode调试与其他

    在项目中设置main接收的参数,模拟终端输入 Product->Scheme->Edit Scheme->Run->Arguments 例: 相当于在终端执行命令:./ac-t ...

  8. VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到

    VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到 添加完之后要等一会儿 等一会儿 等一会儿 就有了

  9. spring boot项目打包成war并在tomcat上运行的步骤

    把spring-boot项目按照平常的web项目一样发布到tomcat容器下 一.修改打包形式 在pom.xml里设置 <packaging>war</packaging> 二 ...

  10. linux下查看项目端口号,杀掉对应端口号的方法

    查看端口号:netstat -anp 结束端口号:sudo iptables -A INPUT -p tcp --dport 8012 -j DROP"