python常用数据结构的常用操作
作为基础练习吧。列表LIST,元组TUPLE,集合SET,字符串STRING等等,显示,增删,合并。。。
#===========List===================== shoplist = ['apple','mango','carrot','banana'] print 'I have ',len(shoplist), ' items to purchase.' print 'These items are:' for item in shoplist: print item, print '\nI also have to buy rice.' shoplist.append('rice') print 'My shopping list is now', shoplist print 'I will sort my list now' shoplist.sort() print 'Sorted shopping list is', shoplist print 'The first item I will buy is', shoplist[0] olditem = shoplist[0] del shoplist[0] print 'I bought the', olditem print 'My shopping list is ',shoplist #=======================Tuple=================== zoo = ('python','elephant','penguin') print 'Number of animals in the zoo is',len(zoo) new_zoo = 'monkey', 'camel', zoo print 'Number of cages in the new zoo is', len(new_zoo) print 'All animals in new zoo are', new_zoo print 'Animals brought from old zoo are',new_zoo[2] print 'Last animal brought from old zoo is', new_zoo[2][2] print 'Number of animal in the new zoo is ',\ len(new_zoo)-1+len(new_zoo[2]) #===========Dictionary====================== ab = {'Swaroop' :'swaroop@swaroopch.com', 'Larry' :'larry@wall.org', 'Matsumoto':'matz@ruby-lang.org', 'Spammer' :'spammer@hotmail.com' } print "Swaroop's address is", ab['Swaroop'] del ab['Spammer'] print '\nThere are {0} contacts in the address-book.\n'.format(len(ab)) for name, address in ab.items(): print 'Contact {0} at {1}'.format(name, address) ab['Guido'] = 'guido@python.org' if 'Guido' in ab: print "\nGuido's address is",ab['Guido'] #=============Sequence============== print 'Item 0 is',shoplist[0] print 'Item 3 is',shoplist[3] print 'Item -2 is',shoplist[-2] print 'Item 1 to 3 is',shoplist[1:3] print 'Item start to end is',shoplist[:] print 'Item step 2 is',shoplist[::2] #=============Set================ bri = set(['brazil','russia','china','india']) print 'india' in bri print 'usa' in bri bric = bri.copy() bric.add('france') print bric.issuperset(bri) bri.remove('russia') print bri & bric #=================References================ print 'Simple Assignment' mylist = shoplist del shoplist[0] print 'shoplist is', shoplist print 'mylist is', mylist print 'Copy by making a full slice' mylist = shoplist[:] del mylist[0] print 'shoplist is', shoplist print 'mylist is', mylist #=====================String================ name = 'Swaroop' if name.startswith('Swa'): print 'Yes, the string starts with Swa' if 'a' in name: print 'Yes, the string contains the string "a"' if name.find('war') != -1: print 'Yes, the string contains the string "war"' delimiter = '_*_' mylist = ['BRAZIL','RUSSIA','INDIA','CHINA'] print delimiter.join(mylist)
输出:
C:\webpy\webpy\Scripts\python.exe C:/pycode/pycode.py
I have 4 items to purchase.
These items are:
apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is ['banana', 'carrot', 'mango', 'rice']
Number of animals in the zoo is 3
Number of cages in the new zoo is 3
All animals in new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin'))
Animals brought from old zoo are ('python', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
Number of animal in the new zoo is 5
Swaroop's address is swaroop@swaroopch.com
There are 3 contacts in the address-book.
Contact Swaroop at swaroop@swaroopch.com
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org
Guido's address is guido@python.org
Item 0 is banana
Item 3 is rice
Item -2 is mango
Item 1 to 3 is ['carrot', 'mango']
Item start to end is ['banana', 'carrot', 'mango', 'rice']
Item step 2 is ['banana', 'mango']
True
False
True
set(['brazil', 'china', 'india'])
Simple Assignment
shoplist is ['carrot', 'mango', 'rice']
mylist is ['carrot', 'mango', 'rice']
Copy by making a full slice
shoplist is ['carrot', 'mango', 'rice']
mylist is ['mango', 'rice']
Yes, the string starts with Swa
Yes, the string contains the string "a"
Yes, the string contains the string "war"
BRAZIL_*_RUSSIA_*_INDIA_*_CHINA
Process finished with exit code 0
python常用数据结构的常用操作的更多相关文章
- Python基本数据结构之文件操作
用word操作一个文件的流程如下: 1.找到文件,双击打开 2.读或修改 3.保存&关闭 用python操作文件也差不多: f=open(filename) # 打开文件 f.write(&q ...
- Python机器视觉编程常用数据结构与示例
本文总结了使用Python进行机器视觉(图像处理)编程时常用的数据结构,主要包括以下内容: 数据结构 通用序列操作:索引(indexing).分片(slicing).加(adding).乘(multi ...
- python常用数据结构讲解
一:序列 在数学上,序列是被排成一排的对象,而在python中,序列是最基本的数据结构.它的主要特征为拥有索引,每个索引的元素是可迭代对象.都可以进行索引,切片,加,乘,检查成员等操作.在py ...
- PYTHON 100days学习笔记007-3:字符串和常用数据结构
目录 Day007:字符串和常用数据结构 1.使用字符串 2.使用列表 3.使用元组 4.使用字典 4.练习 4.1:在屏幕上显示跑马灯文字 4.2 设计一个函数产生指定长度的验证码,验证码由大小写字 ...
- Python学习-第二天-字符串和常用数据结构
Python学习-第二天-字符串和常用数据结构 字符串的基本操作 def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1 ...
- 【python基础】--常用数据结构
list tuple dict set四种常用数据结构 list list 有序的集合,可以随时添加.删除其中元素值; 支持list嵌套模式, >>> p = ['a','b']&g ...
- python学习之常用数据结构
前言:数据结构不管在哪门编程语言之中都是非常重要的,因为学校的课程学习到了python,所以今天来聊聊关于python的数据结构使用. 一.列表 list 1.列表基本介绍 列表中的每个元素都可变的, ...
- Python之路,Day21 - 常用算法学习
Python之路,Day21 - 常用算法学习 本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...
- Redis 常用数据结构及其控制命令整合
Redis 键值支持5种基本结构,分别是字符串,列表,哈希,集合,有序集合.每一种数据结构都有对应的取值和设值命令,辅助命令,除此之外,还有一些全局命令,用来管理Redis存储的所有 键. 全局命令 ...
随机推荐
- Oracle Dataguard三种保护模式
Oracle Dataguard提供了三种数据保护模式,在此分别总结一下三种数据保护模式的特点. 1.最大保护模式1)这种模式提供了最高级别的数据保护能力:2)要求至少一个物理备库收到重做日志后,主库 ...
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- 杭电 HDU ACM 1698 Just a Hook(线段树 区间更新 延迟标记)
欢迎"热爱编程"的高考少年--报考杭州电子科技大学计算机学院 Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memor ...
- JAVA中JNI的简单使用
了解JNI:JAVA因其跨平台特性而受人们喜爱,也正因此,使得它和本机各种内部联系变得很少,所以JNI(Java Native Interface)就是用来解决JAVA本地操作的一种方式.JAVA通过 ...
- Qt Style Sheets Reference
Qt Style Sheets support various properties, pseudo-states, and subcontrols that make it possible to ...
- Eclipse、MyEclipse使用git插件(egit)
在开发Java.JavaEE等相关程序时,我们会用到Eclipse或者MyEclipse,同时使用到git作为版本控制软件,所以我们需要在这些IDE上集成git插件,而egit正是Eclipse基金会 ...
- Shell基本的命令
ubuntu 中文乱码 如果使用的是 PuTTY,可以通过修改 font, character set 设置来解决. Window -> Appearance -> Font settin ...
- Android集成Mina NIO Socket
Mina简介 Apache MINA(Multipurpose Infrastructure 多功能框架 for Network Applications) 是 Apache 组织一个较新的项目,它为 ...
- HTML <center> 标签
浏览器支持 定义和用法所有浏览器都支持 <center> 标签. 对其所包括的文本进行水平居中. HTML 与 XHTML 之间的差异 在 HTML 4.01 中,center 元素不被赞 ...
- PL/SQL中的变量案例解析
1.标量: ag1: declare v_ename emp.ename%type;--自己称为单变量 begin select ename into v_ename from emp where e ...