【四】Python基础之数据结构:列表

列表

序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。Python有6个序列的内置类型,但最常见的是列表和元组。序列都可以进行的操作包括索引,切片,加,乘,检查成员。

列表基本操作:索引、切片、追加、删除、长度、循环、包含

创建列表:

1 2 3

name_list = ['alex', 'seven', 'eric'] 或 name_list = list(['alex', 'seven', 'eric'])

Python列表脚本操作符

列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。

如下所示:

Python 表达式

结果

描述

len([1, 2, 3])

3

长度

[1, 2, 3] + [4, 5, 6]

[1, 2, 3, 4, 5, 6]

组合

['Hi!'] * 4

['Hi!', 'Hi!', 'Hi!', 'Hi!']

重复

3 in [1, 2, 3]

True

元素是否存在于列表中

for x in [1, 2, 3]: print(x, end=" ")

1 2 3

迭代


Python列表截取与拼接

Python的列表截取与字符串操作类型,如下所示:

L=['Google', 'Runoob', 'Taobao']

操作:

Python 表达式

结果

描述

L[2]

'Taobao'

读取第三个元素

L[-2]

'Runoob'

从右侧开始读取倒数第二个元素: count from the right

L[1:]

['Runoob', 'Taobao']

输出从第二个元素开始后的所有元素

Python列表函数&方法

Python包含以下函数:

序号

函数

1

len(list)列表元素个数

2

max(list)返回列表元素最大值

3

min(list)返回列表元素最小值

4

list(seq)将元组转换为列表

Python包含以下方法:

序号

方法

1

list.append(obj)在列表末尾添加新的对象

2

list.count(obj)统计某个元素在列表中出现的次数

3

list.extend(seq)在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

4

list.index(obj)从列表中找出某个值第一个匹配项的索引位置

5

list.insert(index, obj)将对象插入列表

6

list.pop([index=-1]])移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

7

list.remove(obj)移除列表中某个值的第一个匹配项

8

list.reverse()反向列表中元素

9

list.sort(cmp=None, key=None, reverse=False)对原列表进行排序

10

list.clear()清空列表

11

list.copy()复制列表

列表是有序的,元素可以被修改

test = [11, 22, 33, 44, 55, 22]

################################## List 列表之删除元素 ####################################

1 2 3

del test[1] # 切片删除 del test[1:4] # 删除test[1] ,test[2],test[3] test.clear()  # clear方法,清空test列表中的所有元素,变为空列表 test[]

列表增加

insert、extend、append

列表删除

pop、remove、clear、del

列表统计

len、count

列表排序

reveser、sort、index

# pop方法,弹出test列表指定位置的元素,弹出的元素可以用变量接收
# pop(self, index=None) 参数:index:待弹出元素的下标(可以为空,为空时默认弹出列表最后一个元素)
# v = test.pop(1)

# remove方法,删除(默认从左往右删除第一个符合要求的value,后面符合要求的value不删除)
# remove(self, value) 参数:value:待删除元素
# test.remove(22)

################################## List 列表之增加元素 ####################################

# append方法,在test列表末尾增加一个元素
# append(self, p_object) 参数:p_object:待添加的元素
# test.append(66)
# test.append([77, 88])

# extend方法,扩展test列表 extend(self, iterable) iterable:传入可迭代对象(例如:传入列表,字符串等)
# test.extend([11, '22'])
# extend和append区别如下:
#       test.extend([11, '22']) --> [11, 22, 33, 44, 55, 11, '22']
#       test.extend('abcd') --> [11, 22, 33, 44, 55, 'a', 'b', 'c', 'd']
#       test.append([11, '22']) --> [11, 22, 33, 44, 55, [11, '22']]
#       test.append('abcd') --> [11, 22, 33, 44, 55, 'abcd']

# insert方法,在test列表指定位置插入元素
# insert(self, index, p_object) 参数:index:指定下标位置插入(下标前面插入) p_object:待插入元素
# test.insert(1, '551')

################################## List 列表之查找 ####################################
# index方法,查找元素索引位置(例如:test1 = [11,22,33,22] test.index(22)从左往右找到找到第一个22就停止后面的22就不找了)
# index(self, value, start=None, stop=None) 参数 vale:待查找下标的元素  start:从哪个下标开始查找  stop:寻找结束的下标(查找时不查找结束的下标)
# v = test.index(22, 0, 2) # --> 返回 1

################################## List 列表之其他 ####################################
# copy方法,浅拷贝test列表
# v = test.copy()

# count方法,计算test列表中指定元素出现的次数
# count(self, value) 参数:value:待计算的元素
# v1 = test.count(22)

# reverse方法,test列表反转
# test.reverse()

# sort方法,对test列表内元素进行排序
# sort(self, key=None, reverse=False)参数:reverse:False时从小到大排序,True时从大到小排序
# test.sort(reverse=True)
print(test)

######################################################################################################################################
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

list

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券