博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python常用数据结构的常用操作
阅读量:6501 次
发布时间:2019-06-24

本文共 4112 字,大约阅读时间需要 13 分钟。

作为基础练习吧。列表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', shoplistprint 'I will sort my list now'shoplist.sort()print 'Sorted shopping list is', shoplistprint 'The first item I will buy is', shoplist[0]olditem = shoplist[0]del shoplist[0]print 'I bought the', olditemprint 'My shopping list is ',shoplist#=======================Tuple===================zoo = ('python','elephant','penguin')print 'Number of animals in the zoo is',len(zoo)new_zoo = 'monkey', 'camel', zooprint 'Number of cages in the new zoo is', len(new_zoo)print 'All animals in new zoo are', new_zooprint '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 briprint 'usa' in bribric = bri.copy()bric.add('france')print bric.issuperset(bri)bri.remove('russia')print bri & bric#=================References================print 'Simple Assignment'mylist = shoplistdel shoplist[0]print 'shoplist is', shoplistprint 'mylist is', mylistprint 'Copy by making a full slice'mylist = shoplist[:]del mylist[0]print 'shoplist is', shoplistprint '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

There are 3 contacts in the address-book.

Contact Swaroop at

Contact Matsumoto at
Contact Larry at

Guido's address is

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

转载地址:http://tuxyo.baihongyu.com/

你可能感兴趣的文章
[译稿]同步复制提议 2010-09
查看>>
windows 自动化目录大纲(各企业架构不一样,按需选择)
查看>>
我的友情链接
查看>>
【Visual C++】游戏开发笔记十三 游戏输入消息处理(二) 鼠标消息处理
查看>>
我的友情链接
查看>>
Java 使用 Redis
查看>>
JPA常用注解
查看>>
Java基础学习总结(1)——equals方法
查看>>
Maven学习总结(6)——Maven与Eclipse整合
查看>>
HTML5:理解head
查看>>
oracle
查看>>
java SpringUtil获取bean
查看>>
Centos6.4最小化安装系统初始化脚本
查看>>
PaaS变厚了
查看>>
赛门铁克开启“容灾即服务”时代
查看>>
复杂度归纳--小结
查看>>
基础篇9-python基本数据结构-列表
查看>>
PHP学习笔记 第八讲 Mysql.简介和创建新的数据库
查看>>
【git】git入门之把自己的项目上传到github
查看>>
js获取鼠标位置
查看>>