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存储的所有 键. 全局命令 ...
随机推荐
- adb devices: no permissions
现象: #sudo adb root Error:insufficient permissions for device #adb devices List of devices attached ? ...
- Magician - hdu 5316 (区间查询合并)
题意:有一个区间,然后有两种操作 1. 把a处的值改为b 0,查询区间ab的子序列的最大和,这个比较特殊,子序列里面相邻的数要有不同的奇偶性 ***************************** ...
- 网络智能和大数据公开课Homework3 Map-Reduce编程
Web Intelligence and Big Data by Dr. Gautam Shroff 这门课是关于大数据处理,本周是第一次编程作业,要求使用Map-Reduce对文本数据进行统计.使用 ...
- max key length is 1000 bytes
ALTER TABLE ad_keyword_brand ADD CONSTRAINT w1 UNIQUE (keyword,adasin,brand,page_position,country,ad ...
- oracle创建表空间,用户,授权等
#oracle数据库安装完成后,有两个系统级的用户system 默认密码为 :managersys 默认密码为 :change_on_install #创建表空间tbs_xxxdba,初始大小1G,每 ...
- Android 关于获取摄像头帧数据解码
由于Android下摄像头预览数据只能 ImageFormat.NV21 格式的,所以解码时要经过一翻周折. Camera mCamera = Camera.open(); Camera.Param ...
- Spark RDD API具体解释(一) Map和Reduce
本文由cmd markdown编辑.原始链接:https://www.zybuluo.com/jewes/note/35032 RDD是什么? RDD是Spark中的抽象数据结构类型,不论什么数据在S ...
- Tomcat jdbc pool配置
Tomcat jdbc pool是apache在tomcat7版本中启用的新连接池,用它来解决以往DBCP无法解决的一些问题. Tomcat jdbc pool的优点: (1) tomcat j ...
- 通知中心 NSNotificationCenter
NSNotificationCenter 通知中心提供了一种在程序内广播信息的途径,一个NSNotificationCenter对象本质上是一个通知分发表(notification dispatch ...
- STL之deque双向队列
deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,提供随机访问,deque在接口上和vector非常相似,下面列出deque的常用成员函数: Table 6.9. C ...