码迷,mamicode.com
首页 > 编程语言 > 详细

python手记(4)------列表(操作方法)

时间:2017-07-15 15:48:11      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:dex   efault   python   key   move   序列   tin   列表   链接   

1.增加——append、extend、insert

list.append(item)————————给列表末尾增加条目

list.extend(可迭代对象)——————扩容列表,可增加列表、字符串、元组等

a=[1,3]
In[47]: a.append(5)
In[48]: a
Out[48]: 
[1, 3, 5]
b=[5,6,7]
In[51]: a.extend(b)
In[52]: a
Out[52]: 
[1, 3, 5, 5, 6, 7]

 

list.insert(i,item)————在指定位置前插入项目

a=[1,2,‘haha‘,[34,485,2734],23.45,‘okthen‘]
In[68]: a.insert(3,‘新增加的项‘)
In[69]: a
Out[69]: 
[1, 2, ‘haha‘, ‘新增加的项‘, [34, 485, 2734], 23.45, ‘okthen‘]
In[70]: a.insert(100,‘新增加的项‘)
In[71]: a
Out[71]: 
[1, 2, ‘haha‘, ‘新增加的项‘, [34, 485, 2734], 23.45, ‘okthen‘, ‘新增加的项‘]

当i索引超过最大值,自动加入尾部(同append)

2.hasattr()

hasattr(obj, name, /)
Return whether the object has an attribute with the given name.

判断某种对象是否有迭代性质

In[54]: a=[1,2,3]
In[55]: b=‘ok then‘
In[56]: c=(1,2,3,4)
In[57]: d=345.372
In[58]: ar=hasattr(a,‘__iter__‘)
In[59]: br=hasattr(b,‘__iter__‘)
In[60]: cr=hasattr(c,‘__iter__‘)
In[61]: dr=hasattr(d,‘__iter__‘)
In[62]: print(ar,br,cr,dr)
True True True False

 

3.计数——count():列表中元素出现的次数

help(list.count)
Help on method_descriptor:

count(...)
    L.count(value) -> integer -- return number of occurrences of value

 

4.index()——元素第一次出现时的位置

 

5.删除——remove和pop

help(list.remove)
Help on method_descriptor:

remove(...)
    L.remove(value) -> None -- remove first occurrence of value.
    Raises ValueError if the value is not present.

In[74]: help(list.pop)
Help on method_descriptor:

pop(...)
    L.pop([index]) -> item -- remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.

remove(item)——————删除第一个出现item,无返回值

pop(i(可选))——————删除索引处的项目,索引为可选,无索引便删除最后一个。有返回值:删除的项目

a
Out[75]: 
[1, 2, ‘haha‘, ‘新增加的项‘, [34, 485, 2734], 23.45, ‘okthen‘, ‘新增加的项‘]
In[76]: a.remove(2)
In[77]: a
Out[77]: 
[1, ‘haha‘, ‘新增加的项‘, [34, 485, 2734], 23.45, ‘okthen‘, ‘新增加的项‘]
In[78]: a.pop()
Out[78]: 
‘新增加的项‘

 

6.排序——list.sort():无返回值,将修改原列表,按关键字排序,默认情况从小到大。

help(list.sort)
Help on method_descriptor:

sort(...)
    L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*

 

系统排序函数sorted():对一切可迭代序列都有效(列表,字符串,元组),返回排序后列表

help(sorted)
Help on built-in function sorted in module builtins:

sorted(iterable, key=None, reverse=False)
    Return a new list containing all items from the iterable in ascending order.
    
    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.

 

8.字符串和列表转化:两个分不清的函数str.split()&str.join()

  str.split():根据分隔符将字符串转为列表

a=‘www.sina.com‘
In[95]: a.split(‘.‘)
Out[95]: 
[‘www‘, ‘sina‘, ‘com‘]   #返回值为列表
字符串————————+=列表

 

  str.join(可迭代):用字符串str将可迭代对象链接起来,返回字符串。即列表————————+=字符串

 ‘.‘.join([‘sina‘,‘sina‘,‘com‘])
Out[97]: 
‘sina.sina.com‘

 

python手记(4)------列表(操作方法)

标签:dex   efault   python   key   move   序列   tin   列表   链接   

原文地址:http://www.cnblogs.com/song-raven/p/7182615.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!