Python列表知識補充,python列表知識
1、import this Python之禪,聖經。
>>> import thisThe Zen of Python, by Tim PetersBeautiful 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'] >>>