基础数据类型(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
<span style="color: #0000ff;">def</span> clear(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> L.clear() -&gt; None -- remove all items from L </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> copy(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> L.copy() -&gt; list -- a shallow copy of L </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">return</span><span style="color: #000000;"> []

</span><span style="color: #0000ff;">def</span> count(self, value): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> L.count(value) -&gt; integer -- return number of occurrences of value </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">return</span><span style="color: #000000;"> 0

</span><span style="color: #0000ff;">def</span> extend(self, iterable): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> L.extend(iterable) -&gt; None -- extend list by appending elements from the iterable </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> index(self, value, start=None, stop=None): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;">
    L.index(value, [start, [stop]]) -&gt; integer -- return first index of value.
    Raises ValueError if the value is not present.
    </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">return</span><span style="color: #000000;"> 0

</span><span style="color: #0000ff;">def</span> insert(self, index, p_object): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> L.insert(index, object) -- insert object before index </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> pop(self, index=None): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;">
    L.pop([index]) -&gt; item -- remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.
    </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> remove(self, value): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;">
    L.remove(value) -&gt; None -- remove first occurrence of value.
    Raises ValueError if the value is not present.
    </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> reverse(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> L.reverse() -- reverse *IN PLACE* </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> sort(self, key=None, reverse=False): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> L.sort(key=None, reverse=False) -&gt; None -- stable sort *IN PLACE* </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__add__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self+value. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__contains__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return key in self. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__delitem__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Delete self[key]. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__eq__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self==value. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__getattribute__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return getattr(self, name). </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__getitem__</span>(self, y): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> x.__getitem__(y) &lt;==&gt; x[y] </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__ge__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self&gt;=value. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__gt__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self&gt;value. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__iadd__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Implement self+=value. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__imul__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Implement self*=value. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__init__</span>(self, seq=()): <span style="color: #008000;">#</span><span style="color: #008000;"> known special case of list.__init__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;">
    list() -&gt; new empty list
    list(iterable) -&gt; new list initialized from iterable's items
    # (copied from class doc)
    </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__iter__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Implement iter(self). </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__len__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return len(self). </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__le__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self&lt;=value. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__lt__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self&lt;value. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__mul__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self*value.n </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span><span style="color: #000000;">

@staticmethod </span><span style="color: #008000;">#</span><span style="color: #008000;"> known case of __new__</span>
<span style="color: #0000ff;">def</span> <span style="color: #800080;">__new__</span>(*args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Create and return a new object.  See help(type) for accurate signature. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__ne__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self!=value. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__repr__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return repr(self). </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__reversed__</span>(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> L.__reversed__() -- return a reverse iterator over the list </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__rmul__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self*value. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__setitem__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> Set self[key] to value. </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #0000ff;">def</span> <span style="color: #800080;">__sizeof__</span>(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span>
    <span style="color: #800000;">"""</span><span style="color: #800000;"> L.__sizeof__() -- size of L in memory, in bytes </span><span style="color: #800000;">"""</span>
    <span style="color: #0000ff;">pass</span>

<span style="color: #800080;">__hash__</span> =<span style="color: #000000;"> None

查看

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