python—列表应用
1、内置List方法。
a = ‘asd’
list(a)
返回一个列表,参数是可迭代对象。里面输出的内容还是保持了传入的可迭代对象的元素和顺序。
>>> a = 'asdfg'
>>> list(a)
['a', 's', 'd', 'f', 'g']
>>>
>>> list((1,2,3,4))
[1, 2, 3, 4]
>>> list(12)#因为数字是不可迭代对象
Traceback (most recent call last):
File "" , line 1, in
TypeError: 'int' object is not iterable
>>>
2、xrange和range的具体区别。
2.1、什么是迭代:
迭代的意思:重复很多次地做一些事情。在python中实现了iter方法的对象是可迭代的,比如list、tuple
2.2、xrange的用法:
xrange(开始、结束,步长)
xrange 它生成一个xrange对象
比如我们
a = xrange(1,10)
print type(a)
print a[0]
>>> a = xrange(1,10)
>>> a
xrange(1, 10)
>>> b = range(11,20)
>>>
>>> b
[11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> a[0]
1
>>> b[0]
11
>>>
2.3、比较
range 直接生成一个列表对象
xrange 它是生成一个xrang对象
xrage的用法:
1)、当我们需求操作一个非常大的数据,而且内存比较吃紧的时候,我们可以用xrange来操作省内存。
2)、xrange一般用在循环里面,比如我们只需要操作部分数据的话,而不是返回全部元素来完成操作,推荐用xrange
比如:
for m in range(1000):
if m == 10:
print ‘ssss’
break
for m in xrange(1000):
if m == 10:
print ‘ssss’
break
>>> for m in range(1000):#需要遍历0至999所有值才完成
... if m == 10:
... print 'ssss'
... break
...
ssss
>>>
>>> for m in xrange(1000):#只要遍历0至10的11个元素就可以,非常省内存
... if m == 10:
... print 'ssss'
... break
...
ssss
>>>
3、列表推导式之再应用
3.1、可以做很多例子只要你有想法,例如:……
3.1.2、取出1-100的所有值的平方。
[x*x for x in range(100)]
3.1.3、里面生成东西不只是数字。
生成字符串[‘the %s ’ %d for d in range(10)]
生成元组[(x,y) for x in range(2) for y in range(2)]
生成字典 举例:
dict([(x*x) for x in range(3) for x in range(2)])
root@kali:~/python/laowangpy# python
Python 2.7.3 (default, Mar 14 2014, 11:57:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> [x*x for x in range(1,100)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801]
>>>
>>> ['the %s ' %d for d in xrange(10)]
['the 0 ', 'the 1 ', 'the 2 ', 'the 3 ', 'the 4 ', 'the 5 ', 'the 6 ', 'the 7 ', 'the 8 ', 'the 9 ']
>>>
>>> [(x,y) for x in range(2) for y in range(2)]
[(0, 0), (0, 1), (1, 0), (1, 1)]
>>>
>>> [(x,y) for x in range(2) for y in range(5)]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]
>>>
>>> dict([(x,y) for x in range(3) for y in range(2)])
{0: 1, 1: 1, 2: 1}
>>> dict([(x,y) for x in range(3) for y in range(3)])
{0: 2, 1: 2, 2: 2}
>>>
>>>
4、翻来覆去之在谈引用
a = [‘i’,’am’,’lilei’]
b = a
a[2] = ‘laowang’
print b
这里b的值是什么?
del b
a #a是什么值
>>>
>>> a = ['i','am','lilei']
>>> b = 2
>>> b = a
>>> b
['i', 'am', 'lilei']
>>> a
['i', 'am', 'lilei']
>>> id(b)
3075384300L
>>> id(a)
3075384300L
>>> a[2] = 'laownag'
>>> a
['i', 'am', 'laownag']
>>> b
['i', 'am', 'laownag']
>>> id(a)
3075384300L
>>> id(b)
3075384300L
>>> print b
['i', 'am', 'laownag']
>>> print a
['i', 'am', 'laownag']
>>>
>>>
>>> del b
>>> b
Traceback (most recent call last):
File "" , line 1, in
NameError: name 'b' is not defined
>>> a
['i', 'am', 'laownag']
>>>
>>> id(a)
3075384300L
>>> id(b)
Traceback (most recent call last):
File "" , line 1, in
NameError: name 'b' is not defined
>>>
5、小技巧之再议删除
a = [ ]
del a 基本删除(删除列表对象的引用)
del a[ : ] 大量删除。适合a里数据太多时使用。性能比上面好太多。(清空列表对象里的元素)
>>>
>>> a = [1,2,3,4]
>>> b = a
>>> a
[1, 2, 3, 4]
>>> b
[1, 2, 3, 4]
>>> del a
>>> a
Traceback (most recent call last):
File "" , line 1, in
NameError: name 'a' is not defined
>>> b
[1, 2, 3, 4]
>>>
>>> c = b
>>> b
[1, 2, 3, 4]
>>> c
[1, 2, 3, 4]
>>> id(c)
3075302572L
>>> id(b)
3075302572L
>>>
>>> del c[:]#删除了列表中本身里面所有元素
>>> c
[]
>>> b
[]
>>>
root@kali:~/python/laowangpy# python
Python 2.7.3 (default, Mar 14 2014, 11:57:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [11,22,24,29,30,32]
>>> a.append(28)
>>> a.insert(4,57)
>>> a
[11, 22, 24, 29, 57, 30, 32, 28]
>>> a[11] = 6
Traceback (most recent call last):
File "" , line 1, in
IndexError: list assignment index out of range
>>> a[0] = 6
>>> a
[6, 22, 24, 29, 57, 30, 32, 28]
>>> a.pop(-2)
32
>>> a
[6, 22, 24, 29, 57, 30, 28]
>>> a.sort()
>>> a
[6, 22, 24, 28, 29, 30, 57]
>>>
>#1、使用两种方式输出下面的结果:
>#[1,2,3,4,5,6,7,8]
>>> b = [1,2,3,4,5]
>>> c = b + [6,7,8]
>>> c
[1, 2, 3, 4, 5, 6, 7, 8]
>>> b.extend([6,7,8])
>>> b
[1, 2, 3, 4, 5, 6, 7, 8]
>>>
#2、用列表的2种方式返回结果:[5,4]
>>> b[-4:-5]
[]
>>> b[-4:-5:-1]
[5]
>>> b[-4:-6:-1]
[5, 4]
>>> b[4:6:1]
[5, 6]
>>> b[4:5]
[5]
>>> b[4:5:1]
[5]
>>> d = []
>>> d.append(b.pop(-4))
>>> d
[5]
>>> d.append(b.pop(-5))
>>> d
[5, 3]
>>> d.append(b.pop(-4))
>>> d
[5, 3, 4]
>>> b
[1, 2, 6, 7, 8]
>>> b.insert(2,3)
>>> b
[1, 2, 3, 6, 7, 8]
>>> d
[5, 3, 4]
>>> d.pop(-2)
3
>>> d
[5, 4]
>>>
#3、判断2是否在列表里面
>>> b
[1, 2, 3, 6, 7, 8]
>>> 2 in b
True
>>> 9 in b
False
>>>
习题3:
b = [23,45,22,44,25,66,78]
用列表推导完成下面习题:
1 生成所有奇数组成的列表
2 输出结果:[‘the content 23’,’the content 45’]
3 输出结果:[25,47,24,46,27,68,80]
>>> b
[23, 45, 22, 44, 25, 66, 78]
>>> [m for m in b if m % 2 ==1]#取模
[23, 45, 25]
>>> ['the content %s' % m for m in b[0:2:1]]
['the content 23', 'the content 45']
>>> b
[23, 45, 22, 44, 25, 66, 78]
>>> [m + 2 for m in b ]#所有数字加2
[25, 47, 24, 46, 27, 68, 80]
>>>
习题4:
用range方法和列表推导的方法生成列表:
[11,22,33]
>>> range(11,34,11)#起始值11,终点值34,步长11
[11, 22, 33]
>>> [m*11 for m in range(1,4,1)]#起始值1,终点值4,步长1,然后所有值乘以11
[11, 22, 33]
>>>
习题5:
已知元组:a = (1,4,5,6,7)
1 判断元素4是否在元组里
2 把元素5修改成8
>>> a = (1,4,5,6,7)#元组的元素是无法修改的,因此先转换成list()列表,在列表中添加元素,然后通过tuple()属性转换成新的元组。
>>> 4 in a
True
>>> c = list(a)
>>> c
[1, 4, 5, 6, 7]
>>> c[2] = 8
>>> c
[1, 4, 8, 6, 7]
>>> d = tuple(c)
>>> d
(1, 4, 8, 6, 7)
>>>
习题6:
已知集合:setinfo = set(‘acbdfem’)和集合finfo=set(‘sabcdef’)
1 添加字符串对象’abc’到集合setinfo
2 删除集合setinfo里面的成员m
3 求2个集合的交集和并集
>>> setinfo = set('acdbfem')
>>> setinfo
set(['a', 'c', 'b', 'e', 'd', 'f', 'm'])
>>> setinfo.add('abc')
>>> setinfo
set(['a', 'c', 'b', 'e', 'd', 'f', 'm', 'abc'])
>>> setinfo.remove('m')
>>> setinfo
set(['a', 'c', 'b', 'e', 'd', 'f', 'abc'])
>>>
>>> finfo = set('sabcdef')
>>> finfo
set(['a', 'c', 'b', 'e', 'd', 'f', 's'])
>>> setinfo & finfo
set(['a', 'c', 'b', 'e', 'd', 'f'])
>>> setinfo | finfo
set(['a', 'c', 'b', 'e', 'd', 'f', 's', 'abc'])
>>>
习题7:
用字典的方式完成下面一个小型的学生管理系统
1 学生有下面几个属性:姓名、年龄、考试分数包括:语文、数学、英语得分。
比如定义2个同学:
姓名:李明,年龄23,考试分数:语文80,数学75,英语85
姓名:张强,年龄25,考试分数:语文75,数学82,英语78
2 给学生添加一门python课程成绩,李明60分,张强:88分
3 把张强的数学成绩由82分改成89分
4 删除李明的年龄数据
5 对张强同学的课程分数按照从低到高排序输出。
6 外部删除学生所在的城市属性,不存在返回字符串beijing
>>> studentinfo ={"liming":{'name':'李明','age':25,'fenshu':{'chinese':80,'math':75,'english':85}}}#建立student字典
>>> studentinfo
{'liming': {'fenshu': {'math': 75, 'chinese': 80, 'english': 85}, 'age': 25, 'name': '\xe6\x9d\x8e\xe6\x98\x8e'}}
>>> studentinfo['zhangqiang'] ={'name':'张强','age':23,'fenshu':{'chinese':75,'math':82,'english':78}}#给student字典增加元素
>>> studentinfo
{'liming': {'fenshu': {'math': 75, 'chinese': 80, 'english': 85}, 'age': 25, 'name': '\xe6\x9d\x8e\xe6\x98\x8e'}, 'zhangqiang': {'fenshu': {'math': 82, 'chinese': 75, 'english': 78}, 'age': 23, 'name': '\xe5\xbc\xa0\xe5\xbc\xba'}}
>>>
>>> studentinfo['liming']['fenshu']['python'] = 60
>>> studentinfo
{'liming': {'fenshu': {'python': 60, 'math': 75, 'chinese': 80, 'english': 85}, 'age': 25, 'name': '\xe6\x9d\x8e\xe6\x98\x8e'}, 'zhangqiang': {'fenshu': {'math': 82, 'chinese': 75, 'english': 78}, 'age': 23, 'name': '\xe5\xbc\xa0\xe5\xbc\xba'}}
>>> studentinfo['zhangqiang']['fenshu']['python'] = 80
>>> studentinfo
{'liming': {'fenshu': {'python': 60, 'math': 75, 'chinese': 80, 'english': 85}, 'age': 25, 'name': '\xe6\x9d\x8e\xe6\x98\x8e'}, 'zhangqiang': {'fenshu': {'python': 80, 'math': 82, 'chinese': 75, 'english': 78}, 'age': 23, 'name': '\xe5\xbc\xa0\xe5\xbc\xba'}}
>>>
>>>
>>> studentinfo
{'liming': {'fenshu': {'python': 60, 'math': 75, 'chinese': 80, 'english': 85}, 'age': 25, 'name': '\xe6\x9d\x8e\xe6\x98\x8e'}, 'zhangqiang': {'fenshu': {'python': 80, 'math': 82, 'chinese': 75, 'english': 78}, 'age': 23, 'name': '\xe5\xbc\xa0\xe5\xbc\xba'}}
>>> del studentinfo['liming']['age']
>>> studentinfo
{'liming': {'fenshu': {'python': 60, 'math': 75, 'chinese': 80, 'english': 85}, 'name': '\xe6\x9d\x8e\xe6\x98\x8e'}, 'zhangqiang': {'fenshu': {'python': 80, 'math': 82, 'chinese': 75, 'english': 78}, 'age': 23, 'name': '\xe5\xbc\xa0\xe5\xbc\xba'}}
>>>
>>>
>>> b = studentinfo['zhangqiang']['fenshu'].values()#取出所有课程的成绩
>>> b
[80, 82, 75, 78]
>>> b.sort()
>>> b
[75, 78, 80, 82]
>>>
>>>
>>> studentinfo.pop('city','beijing')#使用pop()是键名与对应的值
'beijing'
>>>