python3数据结构_Python3-数据结构
列表
image.png
image.png
代码实现
List = ['Objective-c','Swift','Python','Java','C++','C']
print('List now content:',List)
print('List length is',len(List))
for item in List:
print('List content: ',item,end=' \n')
List.append('Ruby')//追加元素
print('List now content:',List)
List.sort()//排序
print('List sort content:',List)
print('List first item:',List[0])
firstItem = List[0]
del List[0]//删除元素
print('firstItem is:',firstItem)
print('List finally is:',List)
运行结果
List now content: ['Objective-c', 'Swift', 'Python', 'Java', 'C++', 'C']
List length is 6
List content: Objective-c
List content: Swift
List content: Python
List content: Java
List content: C++
List content: C
List now content: ['Objective-c', 'Swift', 'Python', 'Java', 'C++', 'C', 'Ruby']
List sort content: ['C', 'C++', 'Java', 'Objective-c', 'Python', 'Ruby', 'Swift']
List first item: C
firstItem is: C
List finally is: ['C++', 'Java', 'Objective-c', 'Python', 'Ruby', 'Swift']
列表是可变的,字符串是不可变的
元组 Tuple
元组是不可变的,不可以编辑或更改元组
image.png
代码实现
mouth = ('day 1','day 2','day 3','day 4','day 5')
print('mouth length is :',len(mouth))
new_mouth = mouth,'day 11','day'
print('new mouth is :',new_mouth)
print('old mouth is :',new_mouth[0])
print('last item of old mouth is :',new_mouth[0][len(mouth)-1])
print('new mouth length is :',len(new_mouth))
运行结果
mouth length is : 5
new mouth is : (('day 1', 'day 2', 'day 3', 'day 4', 'day 5'), 'day 11', 'day')
old mouth is : ('day 1', 'day 2', 'day 3', 'day 4', 'day 5')
last item of old mouth is : day 5
new mouth length is : 3
new mouth actually length is 7
image.png
包含一个元素的元组
mouth = ('day1', )//必须在第一个元素后面加逗号,以便程序区分
字典
键值对,key-value。key 只能是不可变得对象,value 是可变和不可变都可以。
样式:dict = {name:joy,age:18}
示例代码
student = {
'name' : 'joy',
'age' : 18,
'stuNo': '036',
'birth':'1994-11-24',
}
print('student message is:',student)
print('student name is:',student['name'])
del student['birth']
print('now student message is {}'.format(student))
for name, age in student.items():
print('name:{} and age :{}'.format(name,age))
student['place'] = 'BeiJing'
if 'place' in student:
print('now student message is {}'.format(student))
输出结果
student message is: {'name': 'joy', 'age': 18, 'stuNo': '036', 'birth': '1994-11-24'}
student name is: joy
now student message is {'name': 'joy', 'age': 18, 'stuNo': '036'}
name:name and age :joy
name:age and age :18
name:stuNo and age :036
now student message is {'name': 'joy', 'age': 18, 'stuNo': '036', 'place': 'BeiJing'}
image.png
序列
image.png
代码实现
List = ['Objective-c','Swift','Python','Java','C++','C']
new_item = 'single Language'
print('item 0 is:',List[0])
print('item 3 is:',List[3])
print('item -2 is:',List[-2])
print('item -1 is:',List[-1])
print('new_item is:',new_item[7])
print('===========================')
# 列表截取
print('item 1-3:',List[1:3])
print('item 2-end is:',List[2:])
print('item 1to-1 is:',List[1:-1])
print('item 0-end is:',List[:])
# 字符串截取
print('new_item 7-end is:',new_item[7:])
print('item 0-4:',new_item[0:4])
print('item 4to-4:',new_item[4:-4])
输出结果
item 0 is: Objective-c
item 3 is: Java
item -2 is: C++
item -1 is: C
new_item is: L
===========================
item 1-3: ['Swift', 'Python']
item 2-end is: ['Python', 'Java', 'C++', 'C']
item 1to-1 is: ['Swift', 'Python', 'Java', 'C++']
item 0-end is: ['Objective-c', 'Swift', 'Python', 'Java', 'C++', 'C']
new_item 7-end is: Language
item 0-4: sing
item 4to-4: le Lang
使用索引获取序列中各个项目,“[ ]”中指定序列的制定数组,Python 将返回该序列制定位置的项目,Python计数从0开始。索引计数也可以为负数,负数表示从序列的末尾向首位开始计数。
image.png
还可以设置截取跨度长度
List[ : : 2]//表示从起始-结尾 截取长度为2的项目
image.png
集合
代码实现
stu = set(['jone','tom','jerry','jack'])
print('stu:',stu)
print('jack is in stu? ','jack' in stu)
print('lee is in stu? ','lee' in stu)
stus = stu.copy()
stus.add('jodan')
print('stus:',stus)
print('stus is stu superset? ',stus.issuperset(stu))
print('stus and stu all item? ',stu | stus)
stus.remove('tom')
stus.add('robert')
print('stu:',stu)
print('stus:',stus)
print('stus and stu all item? ',stu | stus)
print('stus and stu intersection item? ',stu & stus)
输出结果
stu: {'jack', 'tom', 'jerry', 'jone'}
jack is in stu? True
lee is in stu? False
stus: {'tom', 'jack', 'jerry', 'jodan', 'jone'}
stus is stu superset? True
stus and stu all item? {'tom', 'jack', 'jerry', 'jodan', 'jone'}
stu: {'jack', 'tom', 'jerry', 'jone'}
stus: {'jack', 'robert', 'jerry', 'jodan', 'jone'}
stus and stu all item? {'tom', 'jack', 'robert', 'jerry', 'jodan', 'jone'}
stus and stu intersection item? {'jack', 'jerry', 'jone'}
set 支持拷贝、增加、删除等基本操作。
引用
创建一个对象,分配给该对象某个变量,变量只会 refer 某个对象,不会代表对象本身。变量名只是指向计算机内存储该对象的内存地址。在引用时候会出现不同的效果。
案例
List = ['Objective-c','Swift','Python','Java','C++','C']
new_list = List
# new_list 只是指向 List 地址的名称,List 改变,new_list也会随之改变
List.remove("C")
print('List:',List)
print('newList:',new_list)
# new_list 与 List 指向同一对象
copy_list = List[:]
# 通过切片 复制出一份 List 的副本
print('copy_list:',copy_list)
del copy_list[0]
print('List:',List)
print('newList:',new_list)
print('copy_list:',copy_list)
输出结果
List: ['Objective-c', 'Swift', 'Python', 'Java', 'C++']
newList: ['Objective-c', 'Swift', 'Python', 'Java', 'C++']
copy_list: ['Objective-c', 'Swift', 'Python', 'Java', 'C++']
List: ['Objective-c', 'Swift', 'Python', 'Java', 'C++']
newList: ['Objective-c', 'Swift', 'Python', 'Java', 'C++']
copy_list: ['Swift', 'Python', 'Java', 'C++']
如果想操作而又不影响源数据的情况下,使用 “ List[ : ] ” 这样得到的序列是拷贝出一份副本,互相操作不会影响;如果采用 “ a=b ”,则操作会造成联动,慎用!
字符串补充
代码实现
List = ['Objective-c','Swift','Python','Java','C++','C']
str = 'python'
if str.startswith('py'):
# 字符串以什么开头
print('str start at \'py\'')
if 'on' in str:
# 字符串包含
print('str contain \'on\'')
if str.find('th') != -1:
# 查找字符串的位置,如果找不到则返回"-1"
print('\'th\' in str location is:',str.find('th'))
otherStr = ' ~ '
print('List change str is:',otherStr.join(List))
#otherStr 是作为List 中每个项目的分隔符,返回一个更大的字符串
输出结果
str start at 'py'
str contain 'on'
'th' in str location is: 2
List change str is: Objective-c ~ Swift ~ Python ~ Java ~ C++ ~ C
python3数据结构_Python3-数据结构相关推荐
- 自考数据结构和数据结构导论_我跳过大学自学数据科学
自考数据结构和数据结构导论 A few months back, I decided I wanted to learn data science. In order to do this, I sk ...
- 数据结构 python的书推荐-为什么程序员一定要学数据结构?数据结构书单推荐~...
原标题:为什么程序员一定要学数据结构?数据结构书单推荐~ 来自:程序员书库(ID:OpenSourceTop) 人们最初使用计算机是用来处理简单的数值计算问题,当你使用计算机来处理一个问题时,一般经过 ...
- python需要学数据结构吗_Python新手学习基础之数据结构-对数据结构的认知
什么是数据结构? 数据结构是指:相互之间存在着一种或多种关系的数据元素的集合和该集合中数据元素之间的关系组成. 举个列子来理解这个数据结构: 数据可以比作是书本, 数据结构相当于书架,书存放在书架上, ...
- mysql存储map数据结构_map数据结构
Go map实现原理 - 恋恋美食的个人空间 - OSCHINA - 中文开源技术交流社区 https://my.oschina.net/renhc/blog/2208417 // A header ...
- 数据结构 (一) ----- 数据结构基本概念基于数组实现线性表
相关文章: <数据结构 (一) ----- 数据结构基本概念&基于数组实现线性表> 文章目录 数据结构基本概念 一.逻辑结构 二.存储结构 三.数据结构定义 四.数据结构的通用的几 ...
- Day739.GEO经纬度数据结构自定义数据结构 -Redis 核心技术与实战
GEO经纬度数据结构&自定义数据结构 Hi,我是阿昌,今天学习记录的是关于GEO经纬度数据结构&自定义数据结构的内容,感谢您的关注和观看. Redis 的 5 大基本数据类型:Stri ...
- 考研数据结构笔记--数据结构和算法的基本概念
考研数据结构笔记--数据结构和算法的基本概念 数据结构的基本概念 算法的基本概念 数据结构的基本概念 数据 数据是对客观事物的符合表示,在计算机科学中是指所有能输入到计算机中并且被计算机程序处理的符合 ...
- 数据结构——初识数据结构
数据结构(计算机408考研科目) 首先我们提出一个问题:数据结构是在学习什么? 带着这个疑问我们来观察一下我们身边的世界. 从一日三餐,吃穿住行来看: 1.我们有时不想做饭时,我们可以打开手机去点外卖 ...
- 【大话数据结构】——-数据结构
数据结构,顾名思义:就是计算机存储.组织数据的方式. 定义: 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合. 使用数据结构的好处: 通常情况下,精心选择的数据结构可以带来更高的运行或者存 ...
- python3有多少个关键字排序_python3 数据结构和算法-- 通过某个关键字排序一个字典列表...
通过某个关键字排序一个字典列表 问题 你有一个字典列表,你想根据某个或某几个字典字段来排序这个列表. 解决方案 通过使用 operator 模块的 itemgetter 函数,可以非常容易的排序这样的 ...
最新文章
- 过河问题matlab建模,matlab三对夫妻过河问题
- hdu1010(小狗逃迷宫)
- 饿了么CTO张雪峰:允许90后的技术人员“浮躁“一点
- export default (imported as router) was not found_小学生必须知道的英语语法知识:as…as的七大用法...
- upload-labs_pass 9_::$DATA_绕过
- 动态添加TemplateField列
- 怎么解锁blockinput锁定的键盘_电脑键盘上的三个灯分别是什么作用?虽然很简单,但是很实用...
- 实现酒店无线覆盖和无线漫游
- FOI 冬令营 Day6
- C#:Winform 打字测速程序 Typer
- 解决MERCURY弹出上网公告的窗口方法
- NBA表格_巅峰乔丹的进攻水平,是NBA常规赛历史最佳么?
- js输出26个英文字母
- listview qt 选中内容_演练: 操作Qt应用中的QListView
- Java核心技术卷一(百度云资源)
- 09-Scrum过程-评审会(Review Meeting) 反思会(Retrospective Meeting)
- Purism FAQ
- php商城建站,php商城建站系统
- 少儿编程 电子学会图形化编程等级考试Scratch三级真题解析(选择题)2022年9月
- 自动化专业本科学习笔记
热门文章
- 使用HTTP/HTTPS 协议,请求第三方
- PHP 执行shell 脚本,常见问题
- android 烟雾动画,android使用粒子动画实现炊烟袅袅的效果
- 如何巧妙运用技巧涨粉
- dedecms教程:二次开发时使用{dede:arclist},{dede:list}
- 企业如何降低ERP运维成本又能增加ERP系统和业务的灵活性?
- Arduino UNO利用外部中断测量RC接收器PPM脉宽
- python实现简单的办公自动化
- FPGA笔记 Cyclone IV 供电电源
- 论文悦读(3)——NVM文件系统之ZoFS(SOSP‘19)文件系统