python基础总结(三)之列表

2021/11/11 22:17:38

本文主要是介绍python基础总结(三)之列表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、列表的格式

列表是由一对中括号包裹起来的元素,称为列表。为可变数据类型,列表中的每一个数据称为元素。可以使用下标来获取元素和对元素切片吧。
1、定义列表的格式:[元素1,元素2, …, 元素n]
2、t = [ ]
3、t = list(可迭代对象)
列表中的元素可以是不同的数据类型

二、列表的操作

2.1 增加元素

append方法是在列表的最后增加一个元素,列表的地址在列表增加或删除前后是不变的。

2.1.1 append方法

append源码解释:

    def append(self, *args, **kwargs): # real signature unknown
        """
        Append a single item to the end of the bytearray.
        
          item
            The item to be appended.
        """
        pass

示例代码:

heros = ['阿珂', '嬴政', '韩信', '露娜', '后羿', '亚瑟', '李元芳']
print(id(heros))
# append增加元素, 在列表的最后面追加一个元素
heros.append('黄忠')
print(heros)
print(id(heros))

# 输出结果:
['阿珂', '嬴政', '韩信', '露娜', '后羿', '亚瑟', '李元芳']
1806544791560
['阿珂', '嬴政', '韩信', '露娜', '后羿', '亚瑟', '李元芳', '黄忠']
1806544791560

2.1.2 insert方法

insert方法是在列表的任意位置插入元素,insert在末尾插入元素时,地址不变。当在其他位置插入或删除元素时,列表的地址会发生改变,所以一般不建议使用。

insert源码解释:

   def insert(self, *args, **kwargs): # real signature unknown
        """
        Insert a single item into the bytearray before the given index.
        
          index
            The index where the value is to be inserted.
          item
            The item to be inserted.
        """
        pass

示例代码:

# insert增加元素,index表示下标,在那个位置插入数据,object表示对象,具体插入的数据
heros.insert(3, '李白')
print(heros)
print(id(heros))

# 输出结果:
['阿珂', '嬴政', '韩信', '李白', '露娜', '后羿', '亚瑟', '李元芳', '黄忠']
2044774311880

2.1.3 extend方法

extend方法是在列表的末尾添加一个可迭代的对象
extend源码解释:

    def extend(self, *args, **kwargs): # real signature unknown
        """ Extend list by appending elements from the iterable. """
        pass

示例代码:

# extend增加元素,需要一个可迭代对象
x = ['马克菠萝', '米莱迪', '狄仁杰']
heros.extend(x)
print(heros)
print(id(heros))

# 输出结果:
['阿珂', '嬴政', '韩信', '李白', '露娜', '后羿', '亚瑟', '李元芳', '黄忠', '马克菠萝', '米莱迪', '狄仁杰']
2876149655560

2.2 删除元素

2.2.1 pop方法

pop方法源码解释:

    def pop(self, *args, **kwargs): # real signature unknown
        """
        Remove and return a single item from B.
        
          index
            The index from where to remove the item.
            -1 (the default value) means remove the last item.
        
        If no index argument is given, will pop the last item.
        """
        pass

pop方法默认删除最后一个元素,并返回被删除元素。如果列表为空或者索引超出范围,将会抛出索引错误提示

# pop方法默认删除最后一个元素,并返回删除元素的值
pop_list = heros.pop()
print(pop_list)

# 输出结果:
阿珂

# 也可指定删除元素的索引进行删除
pop_list = heros.pop(1)
print(pop_list)

# 输出结果:
米莱迪

2.2.2 remove方法

remove方法的参数是要删除的值
示例代码:

# remove方法的参数是要删除的值
heros.remove("露娜")
print(heros)

# 输出结果:
['狄仁杰', '马克菠萝', '黄忠', '李元芳', '亚瑟', '后羿', '李白', '韩信', '嬴政', '阿珂']

2.2.3 clear方法

clear方法是清除列表中的所有元素,清除后列表变为一个空列表。

# clear方法
heros.clear()
print(heros)

# 输出结果:
[]

2.3 修改元素

修改元素只需要将对应的下标等于新的值即可

heros[1] = '张三'
print(heros)

# 输出结果:
['阿珂', '张三', '韩信', '露娜', '后羿', '亚瑟', '李元芳', '后羿']

2.4 查找元素

2.4.1 通过索引查找元素

print(heros[1])

# 输出结果:
嬴政

2.4.2 index方法

index方法:返回查找元素的下标,元素不存在则报错

# index方法:返回查找元素的下标,元素不存在则报错
print(heros.index('韩信'))

# 输出结果:
2

2.5 反转列表

reverse方法则是将列表进行反转

heros.reverse()
print(heros)

# 输出结果:
['后羿', '李元芳', '亚瑟', '后羿', '露娜', '韩信', '嬴政', '阿珂']

2.6 列表排序

2.6.1 sort方法

sort方法会改变列表,产生一个新的列表

heros.sort()
print(heros)

2.6.2 sorted方法

sorted方法是Python的一个全局排序方法,会产生一个新的列表,原来的列表不变

sorted_list = sorted(heros)
print(sorted_list)


这篇关于python基础总结(三)之列表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程