python教程--列表补遗一

>>> c = ['wonderful', 'yarn', 1, 2, 3]
>>> c
['wonderful', 'yarn', 1, 2, 3]
>>> d = [i * 2 for i in c]
>>> d
['wonderfulwonderful', 'yarnyarn', 2, 4, 6]

 

 enumerate 函数用于遍历序列中的元素以及它们的索引值:

>>> b

 

['i', 'love', 'python', 'barber', 'flounder', 'graceful', 'They']
>>> for c, d in enumerate(b):

 

print(c, d)
0 i
1 love
2 python
3 barber
4 flounder
5 graceful
6 They

 

 

函数,函数(),括号内就是序列,本身;而方法是:序列.方法()

 

list()函数和tuple()函数接受可迭代对象(比如另一个序列)作为参数,并通过浅拷贝数据
来创建一个新的列表或者元组.虽然字符串也是序列类型的,但是它们并不是经常用于list()和
tuple(). 更多的情况下,它们用于在两种类型之间进行转换,比如你需要把一个已有的元组转
成列表类型的(然后你就可以修改它的元素了),或者相反.

>>> b

 

['i', 'love', 'python', 'barber', 'flounder', 'graceful', 'They']
>>> c = ('confront', 'drowsy', 6.77)
>>> list(c)
['confront', 'drowsy', 6.77]
>>> c
('confront', 'drowsy', 6.77)
>>> tuple(b)
('i', 'love', 'python', 'barber', 'flounder', 'graceful', 'They')
>>> b
['i', 'love', 'python', 'barber', 'flounder', 'graceful', 'They']

 

posted @ 2016-12-06 15:48  月光边境Eric  阅读(97)  评论(0编辑  收藏  举报