python中列表的简单用法

1.定义list 

[python]
  1. >>> li = ["a", "b", "mpilgrim", "z", "example"]  
  2. >>> li  
  3. ['a', 'b', 'mpilgrim', 'z', 'example']  
  4. >>> li[0]                                        
  5. 'a'  
  6. >>> li[4]                                        
  7. 'example'  

2.负的list 索引

[python]
  1. >>> li  
  2. ['a', 'b', 'mpilgrim', 'z', 'example']  
  3. >>> li[-1]  
  4. 'example'  
  5. >>> li[-3]  
  6. 'mpilgrim'  
  7. >>> li  
  8. ['a', 'b', 'mpilgrim', 'z', 'example']  
  9. >>> li[1:3]   
  10. ['b', 'mpilgrim']  
  11. >>> li[1:-1]  
  12. ['b', 'mpilgrim', 'z']  
  13. >>> li[0:3]   
  14. ['a', 'b', 'mpilgrim']  

3.向 list 中增加元素

[python]
  1. >>> li  
  2. ['a', 'b', 'mpilgrim', 'z', 'example']  
  3. >>> li.append("new")                
  4. >>> li  
  5. ['a', 'b', 'mpilgrim', 'z', 'example', 'new']  
  6. >>> li.insert(2, "new")             
  7. >>> li  
  8. ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']  
  9. >>> li.extend(["two", "elements"])  
  10. >>> li  
  11. ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']  

4.搜索 list

[python]
  1. >>> li  
  2. ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']  
  3. >>> li.index("example")  
  4. 5  
  5. >>> li.index("new")      
  6. 2  
  7. >>> li.index("c")        
  8. Traceback (innermost last):  
  9.   File "<interactive input>", line 1, in ?  
  10. ValueError: list.index(x): x not in list  
  11. >>> "c" in li            
  12. False  

5.从 list 中删除元素

[python]
  1. >>> li  
  2. ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']  
  3. >>> li.remove("z")    
  4. >>> li  
  5. ['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements']  
  6. >>> li.remove("new")  
  7. >>> li  
  8. ['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements']  
  9. >>> li.remove("c")    
  10. Traceback (innermost last):  
  11.   File "<interactive input>", line 1, in ?  
  12. ValueError: list.remove(x): x not in list  
  13. >>> li.pop()          
  14. 'elements'  
  15. >>> li  
  16. ['a', 'b', 'mpilgrim', 'example', 'new', 'two']  

remove 从 list 中删除一个值的首次出现。
remove 仅仅 删除一个值的首次出现。 在这里, 'new' 在 list 中出现了两次, 但 li.remove("new") 只删除了 'new' 的首次出现。
如果在 list 中没有找到值, Python 会引发一个异常来响应 index 方法。 
pop 会做两件事: 删除 list 的最后一个元素, 然后返回删除元素的值。

6.list 运算符

[python] view plain copy
  1. >>> li = ['a', 'b', 'mpilgrim']  
  2. >>> li = li + ['example', 'new']  
  3. >>> li  
  4. ['a', 'b', 'mpilgrim', 'example', 'new']  
  5. >>> li += ['two']                 
  6. >>> li  
  7. ['a', 'b', 'mpilgrim', 'example', 'new', 'two']  
  8. >>> li = [1, 2] * 3               
  9. >>> li  
  10. [1, 2, 1, 2, 1, 2]  

7.使用join链接list成为字符串

[python]
  1. >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}  
  2. >>> ["%s=%s" % (k, v) for k, v in params.items()]  
  3. ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']  
  4. >>> ";".join(["%s=%s" % (k, v) for k, v in params.items()])  
  5. 'server=mpilgrim;uid=sa;database=master;pwd=secret'  

join 只能用于元素是字符串的 list; 它不进行任何的类型强制转换。连接一个存在一个或多个非字符串元素的 list 将引发一个异常。

8.分割字符串

[python]
    1. >>> li = ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']  
    2. >>> s = ";".join(li)  
    3. >>> s  
    4. 'server=mpilgrim;uid=sa;database=master;pwd=secret'  
    5. >>> s.split(";")     
    6. ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']  
    7. >>> s.split(";", 1)  
    8. ['server=mpilgrim', 'uid=sa;database=master;pwd=secret']  
posted @ 2016-05-20 16:27  6随6心6  阅读(621)  评论(0编辑  收藏  举报