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. mybatis 插件安装与使用

    安装 1.在MarketPlace 中搜索 MyBatipse  安装 2.下载MyBatipse 插件 使用 ......

  2. JMeter学习(十四)JMeter函数学习(转载)

    转载自 http://www.cnblogs.com/yangxia-test JMeter函数是一些能够转化在测试树中取样器或者其他配置元件的域的特殊值.一个函数的调用就像这样:${_functio ...

  3. springboot jpa sql查询与传值

    public interface ARepository extends PagingAndSortingRepository<A, APK>, JpaSpecificationExecu ...

  4. jQuery 设置/获取样式

    参考 http://www.w3school.com.cn/jquery/jquery_css.asp $("#a").css("height"); $(&qu ...

  5. Centos 7 MariaDB Galera cluster 部署

    一.安装环境准备 1.系统: CentOS Linux release 7.4.1708 (Core) 2.hosts 10.6.32.51 openstack1 10.6.32.52 opensta ...

  6. 201. Bitwise AND of Numbers Range (Bit)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND(按位与) of all nu ...

  7. PHPlaravel中从数据库中选择数据是增加时间条件及各种条件

    注:附加条件后要加get函数. 1.public function getForDataTable($startTime,$endTime){ return $this->query() -&g ...

  8. 【环境配置】本地配置sublime text以及和远程linux设置sftp

    工具: sublime text 2(mac版) 远程linux(centos 7系) securCRT(for mac) [本地安装并配置securCRT(for mac)] 关于配置: 1.解决终 ...

  9. Linux系统挂载只读改成读写

    1.mount命令可用于查看哪个模块输入只读,一般显示为: [root@localhost ~]# mount /dev/cciss/c0d0p2 on / type ext3 (rw) proc o ...

  10. Bootstrap(3) 表格与按钮

    1.表格 基本格式,实现基本的表格样式 <table class="table"> <thead> <tr> <th>编号</ ...