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

Python 列表常用功能详解

时间:2016-01-28 16:38:59      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

class list(object):
"""
list() -> new empty list
list(iterable) -> new list initialized from iterable‘s items
"""
def append(self, p_object): # real signature unknown; restored from __doc__
""" L.append(object) -> None -- append object to end """
pass
在列表的末尾追加一个元素
example:
new_list = [‘andy‘,‘rain‘]
new_list.append(‘hello‘)
print(new_list)

result:
[‘andy‘, ‘rain‘, ‘hello‘]

def clear(self): # real signature unknown; restored from __doc__
""" L.clear() -> None -- remove all items from L """
pass
清空一个列表
example:
new_list = [‘andy‘,‘rain‘]
new_list.clear()
print(new_list)

result:
[]

def copy(self): # real signature unknown; restored from __doc__
""" L.copy() -> list -- a shallow copy of L """
return []
复制一个列表
example:
new_list = [‘andy‘,‘rain‘]
news_list = new_list.copy()
print(news_list)

result:
[‘andy‘, ‘rain‘]

def count(self, value): # real signature unknown; restored from __doc__
""" L.count(value) -> integer -- return number of occurrences of value """
return 0
统计列表列表中同一个元素有多少个
example:
new_list = [‘andy‘,‘rain‘,‘andy‘]
print(new_list.count(‘andy‘))

result:
2

def extend(self, iterable): # real signature unknown; restored from __doc__
""" L.extend(iterable) -> None -- extend list by appending elements from the iterable """
pass
在现有的列表中追加列表、元组、字符串
example:
new_list = []
new_list.extend(‘andy‘)
print(new_list)

new_list = []
new_list.extend([‘andy‘,‘hello‘])
print(new_list)

result:
[‘a‘, ‘n‘, ‘d‘, ‘y‘]
[‘andy‘, ‘hello‘]

def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
获取列表中某个元素的索引位置
example:
new_list = [‘andy‘,‘rain‘]
print(new_list.index(‘andy‘))

result:
0

def insert(self, index, p_object): # real signature unknown; restored from __doc__
""" L.insert(index, object) -- insert object before index """
pass
在列表指定位置插入一个元素(必须要指定一个插入位置、即索引)
example:
new_list = [‘andy‘,‘rain‘]
new_list.insert(0,‘first‘)
print(new_list)

result:
[‘first‘, ‘andy‘, ‘rain‘]


def pop(self, index=None): # real signature unknown; restored from __doc__
"""
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
"""
pass
删除列表元素:指定所以、删除指定元素;不指定元素默认删除最后一个元素
example:
new_list = [‘andy‘,‘rain‘,‘andy‘]
new_list.pop(0)
print(new_list)

new_list = [‘andy‘,‘rain‘,‘andy‘]
new_list.pop()
print(new_list)

result:
[‘rain‘, ‘andy‘]
[‘andy‘, ‘rain‘]

def remove(self, value): # real signature unknown; restored from __doc__
"""
L.remove(value) -> None -- remove first occurrence of value.
Raises ValueError if the value is not present.
"""
pass
删除指定元素的左数第一个
example:
new_list = [‘andy‘,‘rain‘,‘andy‘]
new_list.remove(‘andy‘)
print(new_list)

result:
[‘rain‘, ‘hello‘]

def reverse(self): # real signature unknown; restored from __doc__
""" L.reverse() -- reverse *IN PLACE* """
pass
#列表反转
new_list = [1,2,3,4,5,6,7,8,9,10]
new_list.reverse()
print(new_list)

result:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
""" L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
pass
#列表排序: 特殊字符优先级最高、然后是数字、然后是大些字母、然后是小写字母
new_list = [5,1,3,2,6,7,9,8,10,4]
new_list.sort()
print(new_list)

new_list = [‘z‘,‘!‘,‘*‘,‘2‘,‘a‘,‘~‘,‘b‘,‘A‘,‘c‘,‘a‘,‘1‘,‘/‘,‘?‘,‘%‘]
new_list.sort()
print(new_list)

result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[‘!‘, ‘%‘, ‘*‘, ‘/‘, ‘1‘, ‘2‘, ‘?‘, ‘A‘, ‘a‘, ‘a‘, ‘b‘, ‘c‘, ‘z‘, ‘~‘]



def __add__(self, *args, **kwargs): # real signature unknown
""" Return self+value. """
pass

def __contains__(self, *args, **kwargs): # real signature unknown
""" Return key in self. """
pass

def __delitem__(self, *args, **kwargs): # real signature unknown
""" Delete self[key]. """
pass

def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass

def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass

def __getitem__(self, y): # real signature unknown; restored from __doc__
""" x.__getitem__(y) <==> x[y] """
pass

def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass

def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass

def __iadd__(self, *args, **kwargs): # real signature unknown
""" Implement self+=value. """
pass

def __imul__(self, *args, **kwargs): # real signature unknown
""" Implement self*=value. """
pass

def __init__(self, seq=()): # known special case of list.__init__
"""
list() -> new empty list
list(iterable) -> new list initialized from iterable‘s items
# (copied from class doc)
"""
pass

def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass

def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass

def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass

def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass

def __mul__(self, *args, **kwargs): # real signature unknown
""" Return self*value.n """
pass

@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass

def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass

def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass

def __reversed__(self): # real signature unknown; restored from __doc__
""" L.__reversed__() -- return a reverse iterator over the list """
pass

def __rmul__(self, *args, **kwargs): # real signature unknown
""" Return self*value. """
pass

def __setitem__(self, *args, **kwargs): # real signature unknown
""" Set self[key] to value. """
pass

def __sizeof__(self): # real signature unknown; restored from __doc__
""" L.__sizeof__() -- size of L in memory, in bytes """
pass

__hash__ = None

Python 列表常用功能详解

标签:

原文地址:http://www.cnblogs.com/RainBower/p/5166562.html

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