Python数据结构

数据结构:数据个体的存储 和 数据个体与个体之间关系的存储。

Python中有:1.序列 2.映射类型 3.无序集合

序列:成员有序排列、通过下标偏移量进行访问。元组、字符串、列表。

映射:依据键索引。字典。

集合:无序,没有索引,所以只能增、删、查(For遍历),没有删除某个元素的方法。

python中的数据结构:元组(Tuple)、列表(Lists)、字典(Dictionariy)、集(Set)

一、元组

元组与列表类似,不同的是元组是不可更改类型,但是元组中的列表,字典可更改。意味着不能进行增、删、改、查、排序、倒置等,Python源码中Tuple支持的操作。

# count 统计某个元素出现的次数
def count(self, value): # real signature unknown; restored from __doc__
""" T.count(value) -> integer -- return number of occurrences of value """
return 0 #获取某个值所在位置,start stop 表示可以切片
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0 #获取长度的方法。公共方法,list dict str都可以使用
def len(*args, **kwargs): # real signature unknown
""" Return the number of items in a container. """
pass

1.1元组 常用操作

#1. 元组拼接
tu = (1,2,3,4,5,6,7,1)
tu1 = tu[0:4] # 元组切出来的还是元组
tu2 = tu[4:]
tu3 = tu1 + tu2
print(tu1) #2. 获取元组长度
print(len(tu)) # #3. 获取元组中 1 出现的次数
print(tu.count(1)) # #4. 返回 4 的索引
print(tu.index(4)) # #5.删除 元组
del tu
print(tu) # NameError

1.2列表

列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如:

li = ['a',[1,2,'a'],True,('a','n','m'),{'键':'值'}]

1.2.1列表的操作

 li = ['a',[1,2,'a'],True,('a','n','m'),{'键':'值'}]

 #1.增
li.append('Python') #尾部添加
li.extend("") #迭代添加
li[5] = 'append' # 索引加载 #2.插入 任意位置插入
li.insert(0,'Study') #3.删
#3.1 按索引删 -> 返回被删除元素
ret = li.pop(-1)
print(ret) #3.2 按元素删 -> 没有返回值
print(li)
li.remove('a')
print(li)
#3.3 清空链表
li.clear()
print(li)
#3.4 删除链表
del li
#4.5切片删
del li[0:] #类似clear()函数 #4.改
#4.1 索引改
li[0] = 'One'
print(li) #4.2
li[0:2] = 'python' #它会把0:2先删除,然后python 一个一个的拆开插入
print(li) #5.查
#5.1 遍历
for i in li:
print(i)
#5.2 切片
li[0:] #6.遍历
# for i in li:
# print(i)
li = [1,2,5,3,6,4,7,12,9,8,11,10]
#7.排序
print(li)
li.sort()#没有返回值,只能操作完,打印
print(li)
#8.倒置
li.reverse()
print(li)

1.3字典

字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据。python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可哈希的。可哈希表示key必须是不可变类型,如:数字、字符串、元组。

dic  = {'name':'Python','version':3.6,'code':3}

#增
'''
1.dic[key] 如果有相同的key,直接覆盖,否则,添加到尾部
2.dic.setdefault()如果key在字典中,则返回其值。如果没有,则插入值为default的key,并返回default。default默认为None。
'''
#dic['RuntimePlatform'] = 'Win10' {'name': 'Python', 'version': 3.6, 'code': 3, 'RuntimePlatform': 'Win10'} #dic.setdefault('ChName','派森') {'name': 'Python', 'version': 3.6, 'code': 3, 'ChName': '派森'} #删
'''
1.dic.pop('key') 删除一个键值对,返回被删除的值
2.dic.pop('key','提示信息') 删除一个key,如果key不存在,返回提示信息
3.clear() 清空dic
4.del 删除链表
5.del dic['key'] 删除一个键值对,不返回任何信息;如果值不存在,则会引发KeyError异常
'''
dic.setdefault('ChName','派森')
#print(dic)
#dic.pop('ChName1') 如果没有第二个参数,会报KeyError #print(dic.pop('ChNames','没有此键')) #del dic # NameError
#del dic['ChName1'] KeyError #dic.clear() {} 清空字典 #随机删除
#dic.popitem() 默认删除最后一个 #改
'''
1.dic[]
2.dic.update({}) 迭代更新,如果key存在,覆盖;不存在添加
'''
#dic['ChName'] = 'paisen' 如果存在就覆盖
dic.update()
print(dic) #查
'''
1.dic['key']
2.for 循环
3.get() 如果key不存在,返回None
4.get('key','提示信息') 如果key不存在,返回提示信息
'''
for k,v in dic.items() :
print(k,v) print(dic.get('name'))
print(dic.get('Platform','没有此键')) #遍历
'''
1.获取所有的键值对
2.获取所有的键
3.获取所有的值
'''
print(dic.items())
print(dic.keys())
print(dic.values()) for k,v in dic.items():
print(k,v) for k in dic:
print(k,dic[k])

二、Python中数据结构-公共函数和遍历总结

公共方法

    def insert(self, index, p_object): # real signature unknown; restored from __doc__
""" L.insert(index, object) -- insert object before index """
pass def clear(self): # real signature unknown; restored from __doc__
""" L.clear() -> None -- remove all items from L """
pass def count(self, value): # real signature unknown; restored from __doc__
""" L.count(value) -> integer -- return number of occurrences of value """
return 0 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0 del in ; not in 切片

遍历

 while i < len(obj):
print(obj[i]) for i in iterable:
print(i)

04_Python Data Structures的更多相关文章

  1. A library of generic data structures

    A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...

  2. The Swiss Army Knife of Data Structures … in C#

    "I worked up a full implementation as well but I decided that it was too complicated to post in ...

  3. 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》

    按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...

  4. Persistent Data Structures

    原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...

  5. Go Data Structures: Interfaces

    refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...

  6. Choose Concurrency-Friendly Data Structures

    What is a high-performance data structure? To answer that question, we're used to applying normal co ...

  7. 无锁数据结构(Lock-Free Data Structures)

    一个星期前,我写了关于SQL Server里闩锁(Latches)和自旋锁(Spinlocks)的文章.2个同步原语(synchronization primitives)是用来保护SQL Serve ...

  8. [CareerCup] 10.2 Data Structures for Large Social Network 大型社交网站的数据结构

    10.2 How would you design the data structures for a very large social network like Facebook or Linke ...

  9. Manipulating Data Structures

    Computer Science An Overview _J. Glenn Brookshear _11th Edition We have seen that the way data struc ...

随机推荐

  1. 北京师范大学校赛C

    https://www.nowcoder.com/acm/contest/submit/0dff89ad7b8444719df155d507f3e1dd?ACMContestId=3&tagI ...

  2. HDU Today(最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)     ...

  3. Python系列之入门篇——HDFS

    Python系列之入门篇--HDFS 简介 HDFS (Hadoop Distributed File System) Hadoop分布式文件系统,具有高容错性,适合部署在廉价的机器上.Python ...

  4. Kafka+Zookeeper集群搭建

    上次介绍了ES集群搭建的方法,希望能帮助大家,这儿我再接着介绍kafka集群,接着上次搭建的效果. 首先我们来简单了解下什么是kafka和zookeeper? Apache kafka 是一个分布式的 ...

  5. JEECG 3.7.2版本发布,企业级JAVA快速开发平台

    JEECG 3.7.2版本发布 -  微云快速开发平台 JEECG是一款基于代码生成器的J2EE快速开发平台,开源界"小普元"超越传统商业企业级开发平台.引领新的开发模式(Onli ...

  6. PHPStorm+PHPStudy新建第一个PHP项目

    img { max-width: 100% } 熟悉了.net的编程,偶尔也来客串一下PHP.前几天闲来无事随便加了一个PHP的开发群,入群之后傻眼了,群里有大小各位程工1600多人,所以决定学习一下 ...

  7. TypeScript装饰器(decorators)

    装饰器是一种特殊类型的声明,它能够被附加到类声明,方法, 访问符,属性或参数上,可以修改类的行为. 装饰器使用 @expression这种形式,expression求值后必须为一个函数,它会在运行时被 ...

  8. JFinal极速开发框架使用笔记(四) _JFinalDemoGenerator实体类生成及映射自动化

    运行_JFinalDemoGenerator生成实体类和映射之前的项目结构: 运行之后: 生成和改变的文件: 实体类: package com.wazn.model; import com.wazn. ...

  9. Mybatis延迟加载和查询缓存

    摘录自:http://www.linuxidc.com/Linux/2016-07/133593.htm 阅读目录 一.延迟加载 二.查询缓存 一.延迟加载 resultMap可以实现高级映射(使用a ...

  10. spring data jpa 学习笔记

    springboot 集成 springData Jpa 1.在pom.xml添加依赖 <!-- SpringData-Jpa依赖--> <dependency <groupI ...