目录
  • 什么是list?
  • list 可以进行哪些操作呢?
    • 动态长度参数传递
  • 参数是否会被函数攥改?
    • list 这种对象能执行啥操作?
      • tuple 转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))
            

            这是代码运行效果:

            我们是可以修改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)
            

            下面是运行效果:

            可以看到,我们之前在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生成的元素构成新列表。

            总结

            本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!