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存储的所有 键. 全局命令 ...
随机推荐
- 对MFC 框架的认识
1.MFC 的概念 微软基础类库(英语:Microsoft Foundation Classes,简称MFC)是一个微软公司提供的类库(class libraries),以C++类的形式封装了Wind ...
- NDK GDB 中打印vector , vector<vector <> >
在android上进行native开发的时候,我们需要用NDK-GDB 对native code进行调试,其中很麻烦的是,我使用的NDK版本是4.0,该版本还不支持用NDK-GDB直接打印vector ...
- Android TabActivity之感叹
(一)前言 在以前一篇帖子讲ams的时候,提了一下TabActivity.当时说它比较特殊就没有下文了,今天重发一篇帖子,跟大家探讨一下TabActivity. 做个假定先: 比如我们最外面的Acti ...
- [转] GDB attach
转:http://blog.csdn.net/wangeen/article/details/14230171 attach是GDB一种重要的debug模式,在MPI程序debug中发挥重要的作用. ...
- ANDROID内存优化(大汇总——中)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 写在最前: 本文的思路主要借鉴了2014年AnDevCon开发者大会的一个演讲PPT,加上 ...
- [Android]android.graphics.Camera实现图像的旋转、缩放,配合Matrix实现图像的倾斜
android.graphics.Camera可以对图像执行一些比较复杂的操作,诸如旋转与绽放,与Matrix可实现图像的倾斜. 个人总结Camera与Matrix的一些区别如下: Camera的ro ...
- Android系统更改状态栏字体颜色
随着时代的发展,Android的状态栏都不是乌黑一片了,在Android4.4之后我们可以修改状态栏的颜色或者让我们自己的View延伸到状态栏下面.我们可以进行更多的定制化了,然而有的时候我们使用的是 ...
- shell编程备忘
1.脚本存放目录 workspace="$(cd "$(dirname "$0")"; pwd)" 2.输出 其中 command 代表指 ...
- requirejs和r.js的心得
requirejs的GitHub:requirejs r.js的GitHub:r.js grunt-contrib-requirejs的GitHub:grunt-contrib-requirejs r ...
- C++关联容器<map>简单总结
C++关联容器<map>简单总结 map提供大小可变的关联容器,基于关联键值高效检索元素值.当你处理键值对的数据是,都可以考虑使用map关联容器. 特点: 大小可变的关联容器,基于关联键值 ...