捋一捋Python中的List(上)

简介: 正式的Python专栏第26篇,同学站住,别错过这个从0开始的文章!

前面学委分享了Tuple,谈到了Tuple跟列表list的关系。


这次我们转移视线到list!


什么是list?

list 是一个序列!一串数据,这个串可以追加数据。


我们可以把它看成大型商场内,儿童游玩串串车,它就像一趟一趟车厢一样,可以挂上去(还能增加车厢)。


这样tuple(元组)就是焊死了的串串车!


在数据结构中,我们也学习过链表,某种程度上list就是python给出的一个实现。


它可以无限的存放数据,并通过下标(从0开始计数)获取特定位置的元素。


说这么多我们看看代码,感受一下:


list_a = [1, 2, 3]
list_b = ['hello','lei','学委', 666]

上面就是python中的list。

list 可以进行哪些操作呢?

我们前面tuple试过 + 和 *, 这些list能做吗?

答案是肯定的。

这里学委复制了前面tuple的代码进行更改:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/31 10:36 下午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : list_demo.py
# @Project : hello
a = [1, 2, 3]
print(a[0])
t = list(a)
print(t[0:2])
print(type(t[0:2]))
# <class 'list'>
t[0] = 22 # list可以修改对应下标的值!
a[0] = 22
mylist = list([a, [2, 3]])
print(mylist)
([22, 2, 3], [2, 3])
mylist[0][0] = 100 #这个可以!真可以!
print(mylist)
print(type(mylist))

动态长度参数传递

def show_info_v2(name, title, *info):
    print("姓名为:", name)
    print("职称为:", title)
    print("其他评价:", info)
show_info_v2('雷学委', '搬砖大师', "热爱技术", "热爱生活")

参数是否会被函数攥改?

我们看看下面的程序即可:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/24 11:39 下午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : func_call.py
# @Project : hello
def compute_v1(list):
    sum = 0
    for x in list:
        sum += x
    list = list + [sum]
    print("新地址:", id(list))
    return sum
def compute_v2(list):
    sum = 0
    for x in list:
        sum += x
    list[0] = list[0] * 100
    return sum
_list = [1, 2, 3, 4, 5]
print("调用计算函数v1之前:", _list)
print("调用计算函数v1之前内存地址:", id(_list))
print(compute_v1(_list))
print("调用计算函数v1之后:", _list)
print("调用计算函数v1之后内存地址:", id(_list))
_list = [1, 2, 3, 4, 5]
print("调用计算函数v2之前:", _list)
print("调用计算函数v2之前内存地址:", id(_list))
print(compute_v2(_list))
print("调用计算函数v2之后:", _list)
print("调用计算函数v2之后内存地址:", id(_list))

这是代码运行效果:

image.png

我们是可以修改list的元素的。

list 这种对象能执行啥操作?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/31 10:36 下午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : list_demo2.py
# @Project : hello
list = [3, 3, 3]
# new_list = list - list #TypeError: unsupported operand type(s) for -: 'listle' and 'listle'
new_list = list + list
print(new_list)
# 学委还是很喜欢三连的,666
new_list = list * 3
print("三连开光过的list:", new_list)
# new_list = list / 3 # 不支持下次一定啊!
print("'666' in new_listle ? ", '666' in new_list)

下面是运行效果:

image.png

可以看到,我们之前在tuple中的操作list也一一支持了。

tuple 转list

直接上代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/31 10:36 下午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : list_demo2.py
# @Project : hello
mylist = [3, 3, 3]
new_list = [x * 2 for x in mylist]
print("加倍过的list:", new_list)
# 从这里开始展示tuple 转list
tup = (6, 6, 6)
new_list = list(tup)
print("把list转list: ", new_list)
if 6 in new_list:
    print("new_list has 6 !")

说这么多,好像漏了点啥?遍历列表还没有展示呢。

这个非常简单,随手就来

list = [ 1, 2, 3]
for x in list:
    do_on_value(x)

就这样,其实上面的展示代码涵盖了,但是没有特别说出来。


就是这一句 ‘new_list = [x * 2 for x in mylist]’ , 直接遍历列表并把每个函数的值都x2生成的元素构成新列表。


总结

list还有很多功能,我们下篇再继续看,这是tuple介绍的文章:https://juejin.cn/post/7025188119427153933


我们看到list 和 tuple 设计上有一定的对称性!


前者用’[]‘来包围元素,后则用’()'来包围元素, 操作上也很相似,甚至可以说非常一致!


目录
相关文章
|
1月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
36 7
|
1月前
|
SQL 机器学习/深度学习 算法
【python】python指南(五):静态类型注解之List
【python】python指南(五):静态类型注解之List
25 0
【python】python指南(五):静态类型注解之List
|
29天前
|
测试技术 索引 Python
Python接口自动化测试框架(基础篇)-- 常用数据类型list&set()
本文介绍了Python中list和set两种数据类型的使用,包括它们的创建、取值、增删改查操作、排序以及内置函数的使用,还探讨了list的比较函数和set的快速去重功能。
17 0
|
2月前
|
Python
|
1月前
|
JSON 算法 算法框架/工具
【python】python指南(十二):Json与dict、list互相转换
【python】python指南(十二):Json与dict、list互相转换
13 0
|
2月前
|
存储 缓存 Python
Python中的列表(List)和元组(Tuple)是两种重要的数据结构
【7月更文挑战第12天】Python中的列表(List)和元组(Tuple)是两种重要的数据结构
36 1
|
2月前
|
存储 索引 Python
【Python】已解决:IndexError: list index out of range
【Python】已解决:IndexError: list index out of range
206 1
|
3月前
|
存储 Python
Python中list, tuple, dict,set的区别和使用场景
Python中list, tuple, dict,set的区别和使用场景
|
3月前
|
存储 安全 Python
Python List深度使用(四)
Python List 是 Python 中非常常用的一种数据类型,它通过数组实现,可以容纳任意类型的元素,并支持动态扩容。在使用 Python List 时,需要充分考虑其优缺点和性能特征,并避免频繁进行添加或删除操作。在多线程多进程中使用 Python List,需要特别注意线程安全和同步问题。通过深入了解 Python List 的特性和使用方法,我们可以更好地应用它来实现我们的需求。
37 4
|
3月前
|
存储 索引 Python
Python教程:深入了解 Python 中 Dict、List、Tuple、Set 的高级用法
Python 中的 Dict(字典)、List(列表)、Tuple(元组)和 Set(集合)是常用的数据结构,它们各自有着不同的特性和用途。在本文中,我们将深入了解这些数据结构的高级用法,并提供详细的说明和代码示例。
46 2