一、创建一个列表
用把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:
>>> name_list = ["root", "gm", "hlr"]
二、访问列表中的值
使用下标索引来访问列表中的值,与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等。
>>> name_list ['root', 'gm', 'hlr']
三、查看列表可进行的操作
>>> help(name_list) ················· >>> dir(name_list) ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
append("STRING"):向列表的末尾追加一个新的值 clear: copy: count("STRING"):统计某字符串或值在列表中出现的次数 extend: index("STRING"):某值在列表中第一次出现的位置 insert(N,"STRING"):向列表的位置N处插入一个新值 pop(obj=list[-1]):删除列表某个位置的值,默认为最后一个值 remove("STRING"):移除列表中某个值的第一个匹配项
reverse():列表元素值反序排列
sort():对列表内的所以值按照ASCII码进行排序
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) -- append object to end """ pass def count(self, value): # real signature unknown; restored from __doc__ """ L.count(value) -> integer -- return number of occurrences of value """ return 0 def extend(self, iterable): # real signature unknown; restored from __doc__ """ L.extend(iterable) -- extend list by appending elements from the iterable """ pass 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 def insert(self, index, p_object): # real signature unknown; restored from __doc__ """ L.insert(index, object) -- insert object before index """ pass 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 def remove(self, value): # real signature unknown; restored from __doc__ """ L.remove(value) -- remove first occurrence of value. Raises ValueError if the value is not present. """ pass def reverse(self): # real signature unknown; restored from __doc__ """ L.reverse() -- reverse *IN PLACE* """ pass def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__ """ L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 """ pass def __add__(self, y): # real signature unknown; restored from __doc__ """ x.__add__(y) <==> x+y """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) <==> y in x """ pass def __delitem__(self, y): # real signature unknown; restored from __doc__ """ x.__delitem__(y) <==> del x[y] """ pass def __delslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__delslice__(i, j) <==> del x[i:j] Use of negative indices is not supported. """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) <==> x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__('name') <==> x.name """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __getslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) <==> x>y """ pass def __iadd__(self, y): # real signature unknown; restored from __doc__ """ x.__iadd__(y) <==> x+=y """ pass def __imul__(self, y): # real signature unknown; restored from __doc__ """ x.__imul__(y) <==> x*=y """ 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): # real signature unknown; restored from __doc__ """ x.__iter__() <==> iter(x) """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() <==> len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) <==> x<y """ pass def __mul__(self, n): # real signature unknown; restored from __doc__ """ x.__mul__(n) <==> x*n """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) <==> x!=y """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() <==> repr(x) """ pass def __reversed__(self): # real signature unknown; restored from __doc__ """ L.__reversed__() -- return a reverse iterator over the list """ pass def __rmul__(self, n): # real signature unknown; restored from __doc__ """ x.__rmul__(n) <==> n*x """ pass def __setitem__(self, i, y): # real signature unknown; restored from __doc__ """ x.__setitem__(i, y) <==> x[i]=y """ pass def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__ """ x.__setslice__(i, j, y) <==> x[i:j]=y Use of negative indices is not supported. """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ L.__sizeof__() -- size of L in memory, in bytes """ pass __hash__ = None
四、append
# 原列表 >>> name_list ['root', 'gm', 'hlr'] # 新增一个root值 >>> name_list.append("root") # 新列表 >>> name_list ['root', 'gm', 'hlr', 'root']
五、count
# 统计root用户在列表中出现的次数 >>> name_list.count("root") 2
# 额外补充,如何删除列表中的root值(此时列表有2个值为root) >>> name_list ['root', 'hlr', 'gm', 'root'] >>> for i in range(name_list.count("root")): ... name_list.remove("root") ... >>> name_list ['hlr', 'gm']
六、index
# 原列表 >>> name_list ['root', 'gm', 'hlr', 'root'] # 查看hlr用户在列表第一次出现的位置 >>> name_list.index("hlr") 2
七、insert
# 原列表 >>> name_list ['root', 'gm', 'hlr', 'root'] # 在列表的位置2处新增一个test用户 >>> name_list.insert(2, "test") # 新列表 >>> name_list ['root', 'gm', 'test', 'hlr', 'root']
八、pop
# 原列表 >>> name_list ['root', 'gm', 'test', 'hlr', 'root'] # 删除列表位置为2的值 >>> name_list.pop(2) 'test' # 新列表 >>> name_list ['root', 'gm', 'hlr', 'root'] # 删除列表最后一个值 >>> name_list.pop() 'root' # 新列表 >>> name_list ['root', 'gm', 'hlr']
九、remove
# 原列表
>>> name_list ['root', 'gm', 'hlr'] # 新增一个gm用户
>>> name_list.append("gm") # 新列表 >>> name_list ['root', 'gm', 'hlr', 'gm'] # 移除第一个gm用户
>>> name_list.remove("gm") # 新列表 >>> name_list ['root', 'hlr', 'gm']
十、reverse
# 原列表 >>> name_list ['root', 'hlr', 'gm'] # 反向排列列表中元素 >>> name_list.reverse() # 新列表 >>> name_list ['gm', 'hlr', 'root']
十一、sort
# 原列表 >>> name_list ['root', 'hlr', 'gm'] # 对列表进行排序 >>> name_list.sort() # 新列表 >>> name_list ['gm', 'hlr', 'root']
更多列表操作
https://www.cnblogs.com/python-gm/p/12395591.html