#-*- coding:utf-8 -*-
# list 和 tuple
#list 是一种有序集合,可以随时添加和删除漆黑中的元素,如列出班里所有同学的名字
classmates=['张三','李四','王五'];
print classmates;
#['\xe5\xbc\xa0\xe4\xb8\x89', '\xe6\x9d\x8e\xe5\x9b\x9b', '\xe7\x8e\x8b\xe4\xba\x94']
#需要将utf-8转为unicode 用decode('string_escape')
print str(classmates).decode('string_escape');
#['张三', '李四', '王五']
listline = ['苹果','香蕉','橘子','梨'];
print ('listline:%s')%str(listline).decode('string_escape');
# listline:['苹果', '香蕉', '橘子', '梨']
#英文不需要转
students=['aa','bb','cc'];
print 'student:%s'%students;
#student:['aa', 'bb', 'cc'];
#-----------------------------list练习1----------------------------------
shoplist = ['apple', 'mango', 'carrot', 'banana'];
print'I have', len(shoplist), 'items to purchase.';
print ('These items are:')
for item in shoplist:
print item
print 'i also hava to buy rice';
shoplist.append('rice');
print 'my shopping list 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 now',shoplist;
# I have 4 items to purchase.
'''These items are:
apple
mango
carrot
banana'''
# i also hava to buy rice
# my shopping list 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 now ['banana', 'carrot', 'mango', 'rice']
'''变量 shoplist 是将要去市场的人的一个购物单。在 shoplist 中,进存储了各项要
买的东西的名字,但你也可以增加任意类型对象,包括数字甚至列表。
使用了 for...in 循环通过列表中的各项来进行重复。到现在,你一定明白了列表也
是一个序列。序列的特殊性会在后一节讨论。
注意 print 函数的 end 关键参数来表示以空格结束输出,而不是通常的换行。
python3 中
print('These items are:', end=' ')
for item in shoplist:
print(item, end=' '
输出These items are: apple mango carrot banana
接下来,用 append 方法来给列表对象增加一项,像前面讨论的那样。然后,通
过简单地将列表传送给 print 语句漂亮地将列表的内容输出,来检查的确给类表增加
了那一项。
然后,用 sort 方法将列表进行排序。理解这一方法影响了列表本身,没有返回修
改后的列表是重要的 —— 这与字符串起作用的方式不同。这就是我们所说的列表是
易变的,字符串是不可变的。
接下来,当我们在市场买完了一项时,我们想将其从列表中移去。用 del 语句来
实现。这儿,我们提到了想要从列表中移去那一项,用 del 语句将其移除。指定想要
移除列表中的第一项因此用 del shoplist[0](记住,Python 中从 0 开始计算)。
如果想知道通过列表对象定义的所有方法,用 help(list) 获取更多细节。'''
#-------------------------list练习2----------------------------------
stu=['a','z','y','d'];
print (u'学生:%s')%stu; #学生:['a', 'z', 'y', 'd']
'''
1.获取list元素的个数
2.用索引访问stu中第三个元素
3.用索引访问stu中倒数第一个元素
4.网stu中末尾加入学生h
5.将学生f插入到索引为1的位置
6.删掉stu末尾的元素
7.删掉索引为1的元素
8.将第索引为2的元素替换为w
9.对stu进行排序
'''
#1---
print len(stu); #4
#2---
print stu[2]; #y
#3--
print stu[-1]; #d
#4--
stu.append('h');
print (u'学生:%s')%stu; #学生:['a', 'z', 'y', 'd', 'h']
#5--
stu.insert(1,'f');
print (u'学生:%s')%stu; #学生:['a', 'f', 'z', 'y', 'd', 'h']
#6--
stu.pop()
print (u'学生:%s')%stu; #学生:['a', 'f', 'z', 'y', 'd']
#7--
'''print stu.pop(1) #f
print (u'学生:%s')%stu #学生:['a', 'z', 'y', 'd']'''
del stu[1];
print (u'学生:%s')%stu; #学生:['a', 'z', 'y', 'd']
'''del xx[]与xx.pop()的区别
pop返回的是删掉的数值,在其前面加print会看的更直观
del []是根据索引删除集合里的数值
'''
#8--
stu[2]='w';
print (u'学生:%s')%stu; #学生:['a', 'z', 'w', 'd']
#9--
stu.sort();
print (u'学生:%s')%stu; #学生:['a', 'd', 'w', 'z']
#--------补充------------
#list 里面的元素的数据类型也可以不同
box = ['Apple',123,True,'二大爷'];
print('box:%s')%str(box).decode('string_escape') #box:['Apple', 123, True, '二大爷']
#list 元素的也可以是另一个list
box=['python',123,['二大爷',True],'java'];
print ('box:%s')%str(box).decode('string_escape'); #box:['python', 123, ['二大爷', True], 'java']
print ('box长度:'),len(box); #box长度: 4
'''box里有四个元素,可以拆开理解
b1 = ['二大爷',True]
b2 = ['python',123,b1,'java']
要拿到'二大爷'可以写成b1[0]或者b2[2][0]
因此box可以看成一个二维数组,类似得还有三维,思维...数组,不过很少用
'''
#如果一个list中什么也没有,就是一个空list,长度是0
box=[]
print('box长度:'),len(box) #box长度: 0
#---------------------------------------------------------------
#---------------------------------------------------------------
#------tuple--元组
#tuple和list很类似,但是tuple一旦初始化就不能修改
stus = ('bb','aa','cc');
print ('学生:'),stus; #学生: ('bb', 'aa', 'cc')
'''stus为tuple不能改变,它没有append(),insert()...等方法,其获取元素的方式和list
一样,能够正常的使用stu[1],stu[-1],但是不能赋值
不可变tuple的意义:因为tuple 不可变,所以代码更安全,如果可能,尽量用tuple代替list
'''
#--------------------------------------
#定义tuple时,tuple元素就必须被确定,定义空tuple,可以写成()
box = (1,2,3);
print ('box:'),box #box: (1, 2, 3);
box = ();
print ('box:'),box; #box: ()
'''当定义只有一个元素的tuple,如果这么定义就是错的
box = (1)
print box
此时定义的不是tuple,而是1这个数字,因为()既可以表示tuple也可以表示数学公式中的
小括号,等价于 temp=1
print temp
所以只有一个元素的tuple定义时必须加逗号,来消除歧义
'''
box = (1,);
print ('box:'),box; #box: (1,)
#---------------可变的box
box = ('a','b','c',['d','e']);
print('box:'),box; #box: ('a', 'b', 'c', ['d', 'e']);
box[3][0] = '二大爷';
print('box:'),str(box).decode('string_escape'); #box: ('a', 'b', 'c', ['二大爷', 'e'])
#此时改变的是list,并不是tuple
#----------------------------------tuple练习-------------------------------
zoo = ('wolf','elephant','dog');
print 'number of animals in the zoo is',len(zoo);
new_zoo = ('monkey','dolphin',zoo);
print 'number of animals in the new_zoo is',len(new_zoo);
print 'all animals in the new_zoo are:',new_zoo;
print 'animals brought from old zoo are:',new_zoo[2];
print 'last animal brought ftom old zoo is:',new_zoo[2][2];
'''
number of animals in the zoo is 3
number of animals in the new_zoo is 3
all animals in the new_zoo are: ('monkey', 'dolphin', ('wolf', 'elephant', 'dog'))
animals brought from old zoo are: ('wolf', 'elephant', 'dog')
last animal brought ftom old zoo is: dog
'''
'''
变量 zoo 是一个元组,我们看到 len 函数可以用来获取元组的长度。这也表明元
组也是一个序列。
由于老动物园关闭了,我们把动物转移到新动物园。因此,new_zoo 元组包含了
一些已经在那里的动物和从老动物园带过来的动物。回到话题,注意元组之内的元组
不会失去它的特性。
我们可以通过一对方括号来指明某个项目的位置从而来访问元组中的项目,就像
我们对列表的用法一样。这被称作索引运算符。我们使用 new_zoo[2] 来访问 new_zoo
中的第三个项目。我们使用 new_zoo[2][2] 来访问 new_zoo 元组的第三个项目的第三
个项目。
'''
list 和 tuple
原文:http://blog.51cto.com/11927232/2054829