作为基础练习吧。列表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', 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', mylistprint '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常用数据结构的常用操作相关推荐

  1. python 传感器数据结构_Python常用的数据结构详解

    数据结构:通俗点说,就是储存大量数据的容器.这里主要介绍Python的4种基本数据结构:列表.字典.元组.集合. 格式如下: 列表:list = [val1,val2,val3,val4],用中括号: ...

  2. 常用数据结构与常用算法,

    1. 常见数据结构 人们进行程序设计时通常关注两个重要问题,一是如何将待处理的数据存储到计算机内存中,即数据表示:二是设计算法操作这些数据,即数据处理.数据表示的本质是数据结构设计,数据处理的本质是算 ...

  3. Python 基础常用数据结构

    2019独角兽企业重金招聘Python工程师标准>>> 常用数据结构 1)元组 元组是一种静态的数据结构,无法修改,若要修改只能重新生成新的元组. 输出结果: 元组元素的获取是通过索 ...

  4. 【Python基础】盘点 Python 10 大常用数据结构(上篇)

    我的施工之路 上图施工计划,已完成专题: 1我的施工计划 2数字专题 3字符串专题 4列表专题 5流程控制专题 6编程风格专题 7函数使用 8.面向对象编程(上篇) 9.面向对象编程(下篇) Pyth ...

  5. 【Python基础】盘点 Python 10 大常用数据结构(下篇)

    我的施工之路 虽然艰辛,却有读者们陪伴 Python 常用数据结构 此专题<盘点Python10大常用数据结构>目录: 学习目的 学习目标 1 list 2 tuple 3 set 4 d ...

  6. Python机器视觉编程常用数据结构与示例

    Python机器视觉编程常用数据结构与示例 本文总结了使用Python进行机器视觉(图像处理)编程时常用的数据结构,主要包括以下内容: 数据结构 通用序列操作:索引(indexing).分片(slic ...

  7. Python 基础(常用数据结构)

    常用数据结构 1)元组 元组是一种静态的数据结构,无法修改,若要修改只能重新生成新的元组. 输出结果: 元组元素的获取是通过索引值去获得的:例如上面的tup1[0]返回apple:另外你可以直接把tu ...

  8. python sorted下标_Python学习教程(Python学习路线):第七天-字符串和常用数据结构

    Python学习教程(Python学习路线):字符串和常用数据结构 使用字符串 第二次世界大战促使了现代电子计算机的诞生,当初的想法很简单,就是用计算机来计算导弹的弹道,因此在计算机刚刚诞生的那个年代 ...

  9. python有必要看数据结构_盘点 Python 10 大常用数据结构(上篇)

    我的施工计划,已完成专题: Python 常用数据结构 学习目的 这个专题,尽量使用最精简的文字,借助典型案例盘点Python常用的数据结构. 如果你还处于Python入门阶段,通常只需掌握list. ...

最新文章

  1. 还是畅通工程(1233 并查集+kruskal)
  2. Linux中的计划任务—Crontab调度重复执行的任务
  3. ObjC: 使用KVO
  4. 在页面制作过程中需要注意事项
  5. 【渝粤教育】广东开放大学 综合英语1 形成性考核 (36)
  6. select默认选中的option_macOS下妙用option按键
  7. 地址总线是单向还是双向_碳纤维布加固为什么选择单向布?
  8. 微信小程序开发:学习笔记[9]——本地数据缓存
  9. Unity3D之UGUI基础4:Button按钮
  10. 1.12 Linux查看用户信息
  11. 一个 Spring Boot 项目该包含哪些?
  12. 计算机参数配置解读,教你看懂电脑配置参数,了解组装电脑基本知识
  13. javaweb实现在线支付功能
  14. 3d游戏计算机怎么配置要求吗,3DMAX软件对电脑的配置要求
  15. Android各种模拟器使用笔记
  16. A fatal exception has occurred.Program will exit。可能是系统装有多个java编程程序。
  17. MapReduce实现订单商品的统计
  18. 计算机中guest用户是灰的,来宾帐户状态不适用呈灰色状
  19. Java 优化方案:设计模式
  20. 怎么验证mysql完整性_MySQL数据库认证高级(一)——数据完整性

热门文章

  1. php去掉字符串的最后一个字符 substr()的用法
  2. 视觉SLAM学习(三)--------SLAM 综述
  3. php常用比较函数区别表
  4. php比较长的configure
  5. Squid下Http头信息优先级
  6. 变量的属性(全局变量、局部变量、动态变量、静态变量等)
  7. 将DBF,XLS,XML,MDB文件导入C#DataGrid的方法
  8. C#编写一个抓网页的应用程序
  9. junit集成Hamcrest测试集合中某个属性是否包含特定值
  10. PyTorch中nn.Module类简介