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

时间: 2024-08-01 18:17:43

python常用数据结构的常用操作的相关文章

python中字典dict常用操作方法实例总结_python

本文实例总结了python中字典dict常用操作方法.分享给大家供大家参考.具体如下: 下面的python代码展示python中字典的常用操作,字典在python开发中有着举足轻重的地位,掌握字典操作相当重要 #创建一空字典 x = {} #创建包含三个项目的字典 x = {"one":1, "two":2, "three":3} #访问其中的一个元素 x['two'] #返回字典中的所有键列表 x.keys() #返回字典中的所有值列表 x.v

浅谈Java中常用数据结构的实现类 Collection和Map_java

线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.本文试图通过简单的描述,向读者阐述各个类的作用以及如何正确使用这些类. Collection ├List │├LinkedList │├ArrayList │└Vector │ └Stack └Set Map ├Hashtable ├HashMap └WeakHashMap Collection接口 Collection是最基本的集合接口,一个C

快速掌握Win8常用触屏手势操作

Win8的一大特色就是支持触屏操作,所以我们有必要介绍一下常用的Win8手势操作,至于特别常规的手势我们就略过了,比如单击.滑动页面等等.下面我们就来看看常用的Win8触屏手势. 拖动应用或文件:单指按住不动并在屏幕上拖动; 打开更多功能选项:单个手指按住不动,此效果类似于鼠标的右键,可以看到更多的选项; 长按打开更多功能选项 缩放应用程序或者屏幕大小:双指之间距离放大或者缩小; 激活应用程序菜单:过从底部或者顶部边缘滑动屏幕,可激活当前应用的菜单; 激活应用程序菜单 打开磁贴菜单:长按应用磁铁

Win8常用触屏手势操作一览

  拖动应用或文件:单指按住不动并在屏幕上拖动; 打开更多功能选项:单个手指按住不动,此效果类似于鼠标的右键,可以看到更多的选项; 长按打开更多功能选项 缩放应用程序或者屏幕大小:双指之间距离放大或者缩小; 激活应用程序菜单:过从底部或者顶部边缘滑动屏幕,可激活当前应用的菜单; 激活应用程序菜单 打开磁贴菜单:长按应用磁铁然后向下轻拉; 关闭应用:按住应用顶部后将页面拖至屏幕底部; 关闭应用 打开Charm超级栏:在屏幕右侧向左滑动手指; 切换应用:手指从屏幕左边向右滑动; 切换应用 打开最近使

SSIS从理论到实战,再到应用(7)----常用的数据类型转换操作

原文:SSIS从理论到实战,再到应用(7)----常用的数据类型转换操作 上期回顾: SSIS从理论到实战,再到应用(6)----SSIS的自带日志功能   在抽取各种应用的数据时候,经常会遇到数据需要转换类型的操作,比如日期在源数据库是dbtime类型,在本地需要用到字符串,这个时候有两种常用的方案来解决: 1.通过语法在源数据库进行类型转换,类似sqlserver的convert(要转换的类型,长度).... 2.利用ssis自带的组件进行类型转换 本期选用第二种方案做演示.(背景,将exc

SQL Server编程系列(2):SMO常用对象的有关操作

原文:SQL Server编程系列(2):SMO常用对象的有关操作 在上一篇周公简单讲述了SMO的一些基本概念,实际上SMO体系结构远不止周公在上一篇中讲述的那么简单,下图是MSDN上给出的一个完整的结构图: 上图是一个完整的涉及到各种关系的结构图.不过它们之间的层次结构关系周公已经在上一篇做了简单概述. 在本篇中周公还是在上一篇的基础上再做稍微深入一点的介绍,在本篇中主要讲述如何获取数据库常见对象信息,并且如何利用SMO来进行一些日常操作:如创建Login,创建数据库.备份数据库和还原数据库.

C#常用工具类——Excel操作类

/// 常用工具类--Excel操作类 /// <para> ------------------------------------------------</para> /// <para> CreateConnection:根据Excel文件路径和EXCEL驱动版本生成OleConnection对象实例</para> /// <para> ExecuteDataSet:执行一条SQL语句,返回一个DataSet对象</para>

Python高级数据结构学习教程

本文虽然不是很深入,不过实例比较多,学习Python高级数据结构的好基础教程. 数据结构的概念 数据结构的概念很好理解,就是用来将数据组织在一起的结构.换句话说,数据结构是用来存储一系列关联数据的东西.在Python中有四种内建的数据结构,分别是List.Tuple.Dictionary以及Set.大部分的应用程序不需要其他类型的数据结构,但若是真需要也有很多高级数据结构可供选择,例如Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint.本文将介绍

JS常用函数和常用技巧小结_javascript技巧

学习和工作的过程中总结的干货,包括常用函数.常用js技巧.常用正则表达式.git笔记等.为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. Ajax请求 jquery ajax函数 我自己封装了一个ajax的函数,代码如下: var Ajax = function(url, type success, error) { $.ajax({ url: url, type: type, dataType: 'json', timeout: 10000, success: fu