基础数据类型(list列表)

初识列表

标准类型
数字
字符串
列表
元组
字典











  

定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素
特性:
    1.可存放多个值
    2.可修改指定索引位置对应的值,可变
    3.按照从左到右的顺序定义列表元素,下标从0开始顺序访问,有序

列表的创建

  list_test=[’lhf‘,12,'ok']
  或
  list_test=list('abc')
  或
  list_test=list([’lhf‘,12,'ok'])

列表的常用操作

索引

lst = [1,'alex','wusir',2]
print(lst[0])
print(lst[1])
print(lst[2])
print(lst[3])

切片

lst = [1,'alex','wusir',2,3,'哪吒']
print(lst[1:3])
print(lst[1:4:2])
print(lst[::-1])

 

追加

lst = [1,'alex','wusir',2]
lst.append('aaa')
print(lst)

删除

复制代码
lst = [1,'alex','wusir',2,'aaa','bbb']

lst.remove('alex')
print(lst)
lst.pop()
print(lst)
lst.pop(0)
print(lst)
lst.clear()
print(lst)
del lst[0]
print(lst)

复制代码

修改

lst = [1,'alex','wusir',2,3,'哪吒']
lst[2] = '武沛齐'
print(lst)

长度

lst = [1,'alex','wusir',2]
print(len(lst))

循环

lst = [1,'alex','wusir',2]
for i in lst:
    print(i)

包含

lst = [1,'alex','wusir',2]
print('alex' in lst)
print('1' in lst)
print('1' not in lst)

 

列表的工厂函数list()

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

def clear(self): # real signature unknown; restored from doc
""" L.clear() -> None -- remove all items from L """
pass

def copy(self): # real signature unknown; restored from doc
""" L.copy() -> list -- a shallow copy of L """
return []

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) -> None -- 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) -> None -- 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, key=None, reverse=False): # real signature unknown; restored from doc
""" L.sort(key=None, reverse=False) -> None -- stable sort IN PLACE """
pass

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

查看

 列表的工厂函数
posted @ 2019-09-16 18:24  若如初见_you  阅读(4405)  评论(0)    收藏  举报