Python列表知识补充

1、import this  Python之禅,圣经。

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

2、title()  使字符串第一个字母大写

>>> s = "cairui"
>>> print(s.title())
Cairui

3、列表的负索引

-1代表最后一个元素,-2代表倒数第二元素

>>> s = ['cairui',123,456,789,'lei']
>>> print(s[-1])
lei
>>> print(s[-2])
789
>>> 

4、range

>>> range(1,5)
[1, 2, 3, 4]
>>> numbers = list(range(1,5))
>>> print(numbers)
[1, 2, 3, 4]
>>> 
>>> i1 = list(range(2,11,2))
>>> print i1
[2, 4, 6, 8, 10]

5、min(列表)  取最小值  max(列表) 取最大值  sum(列表) 求和

6.用切片打印整个列表

>>> players = [1,2,3,4,5]
>>> print(players[:])
[1, 2, 3, 4, 5]
>>> 

  

7、append  在列表末尾添加元素  insert(索引,内容)在第几个索引位置添加元素  

>>> s = ['a',123,456,789]
>>> s.append('rui')
>>> print(s)
['a', 123, 456, 789, 'rui']

['a', 123, 456, 789, 'rui'] >>> s.insert(0,'rui') >>> print(s) ['rui', 'a', 123, 456, 789, 'rui'] >>> 

 

posted @ 2017-09-19 11:08  潇潇、寒  阅读(333)  评论(0编辑  收藏  举报