python数据结构详解
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数据结构详解的更多相关文章
- Python中的高级数据结构详解
这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...
- Python Collections详解
Python Collections详解 collections模块在内置数据结构(list.tuple.dict.set)的基础上,提供了几个额外的数据结构:ChainMap.Counter.deq ...
- redis 五种数据结构详解(string,list,set,zset,hash)
redis 五种数据结构详解(string,list,set,zset,hash) Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存 ...
- Python闭包详解
Python闭包详解 1 快速预览 以下是一段简单的闭包代码示例: def foo(): m=3 n=5 def bar(): a=4 return m+n+a return bar >> ...
- [转] Python Traceback详解
追莫名其妙的bugs利器-mark- 转自:https://www.jianshu.com/p/a8cb5375171a Python Traceback详解 刚接触Python的时候,简单的 ...
- [转]Redis内部数据结构详解-sds
本文是<Redis内部数据结构详解>系列的第二篇,讲述Redis中使用最多的一个基础数据结构:sds. 不管在哪门编程语言当中,字符串都几乎是使用最多的数据结构.sds正是在Redis中被 ...
- python 数据类型详解
python数据类型详解 参考网址:http://www.cnblogs.com/linjiqin/p/3608541.html 目录1.字符串2.布尔类型3.整数4.浮点数5.数字6.列表7.元组8 ...
- Python 递归函数 详解
Python 递归函数 详解 在函数内调用当前函数本身的函数就是递归函数 下面是一个递归函数的实例: 第一次接触递归函数的人,都会被它调用本身而搞得晕头转向,而且看上面的函数调用,得到的结果会 ...
- redis 五种数据结构详解(string,list,set,zset,hash),各种问题综合
redis 五种数据结构详解(string,list,set,zset,hash) https://www.cnblogs.com/sdgf/p/6244937.html redis 与 spring ...
随机推荐
- Jmeter学习(三十二)调试工具Debug Sampler(转载)
转载自 http://www.cnblogs.com/yangxia-test 一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug S ...
- 二分图 最小点覆盖 poj 3041
题目链接:Asteroids - POJ 3041 - Virtual Judge https://vjudge.net/problem/POJ-3041 第一行输入一个n和一个m表示在n*n的网格 ...
- Vue之数据监听存在的问题
Vue之数据监听 当数据监听的是列表时,数据发生改变,不会被监听到. // 用$set修改数组中的数组能够被监听 // app.$set(this.hobby, 0, "爱你哦") ...
- 33. Search in Rotated Sorted Array (Array;Divide-and-Conquer)
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 微信小程序开发-rem转换rpx小工具
实现原理: 对样式进行格式化,然后根据 “rem” 进行拆分,这样就会拆分成一个数组 [str1,str2,str3...,str6], 除了最后一个元素,前边的元素都会以 “rem” 样式的数值结尾 ...
- 【分布式架构】“spring cloud”与“dubbo”微服务的对比
秉承站在巨人的肩膀上,同时我也不想重复制造轮子,我发现了一系列关于“分布式架构”方面,我需要,同时能够解决我的一些疑惑.问题的博客,转载过来,原文链接: http://blog.csdn.net/ ...
- python中logging模块使用
1.logging模块使用场景 在写程序的时候,尤其是大型的程序,在程序中加入日志系统是必不可少的,它能记录很多的信息.刚刚接触python的时候肯定都在用print来输出信息,这样是最简单的输出,正 ...
- Union and Intersection of two sorted lists 并集和交集
跟面试官确认是arrayList还是singly-linked list /* Union 并集:两个升序的list a, b, 返回其并集(升序排序)*/ public class UnionTw ...
- web中CookieUtils的工具类
该类中包含Web开发中对Cookie的常用操作,如需要Copy带走 package com.project.utils; import java.io.UnsupportedEncodingExcep ...
- os模块。笔记
os 模块提供了很多允许你的程序与操作系统直接交互的功能 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目录名:os.listdir() ...