Lesson04_python3之 列表(List)

本文介绍了Python3中的列表(List)定义,包括如何创建包含多种类型元素的列表,并探讨了列表的特殊操作和相关函数功能。通过示例代码展示了列表的操作方法,并给出了运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

列表(List) 定义:

    列表名=[元素1,元素2...]
    如:mylist = ["hello python",1,1.0,True] 
    List是一组有序数据的集合
    List从第一个元素往后看时,第一个元素下标为0,后面的元素依次加1
    List从最后一个元素往前看时,最后一个元素下标为-1,前面的元素依次减1
    List里什么数据都能放

列表(List)的特殊操作:

列表(List)的函数极功能:


代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

mylist1 = [1,2,3]
mylist2 = ['hi',2.3,True]
#list1 与 list2 合并
print("list1 与 list2 合并结果为:",mylist1+mylist2)

#改变list元素
mylist2[0]='你好'
print("mylist2为",mylist2)

#删除元素
del mylist1[0]
print("mylist1为",mylist1)

#增加元素
mylist1.insert(0, 1)
print("mylist1为",mylist1)

#尾部追加
mylist1.append(4)
print(mylist1)

#删除最后一个元素
print("删除mylist1的最后一个元素",mylist1.pop())

#遍历mylist1
for x in mylist1:
    print(x)


运行结果:

list1 与 list2 合并结果为: [1, 2, 3, 'hi', 2.3, True]
mylist2为 ['你好', 2.3, True]
mylist1为 [2, 3]
mylist1为 [1, 2, 3]
[1, 2, 3, 4]
删除mylist1的最后一个元素 4
1
2
3


### Python列表与C++中的等价数据结构 Python中的列表是一种动态数组,能够存储任意类型的元素,并且支持动态扩展和收缩。在C++中,最接近Python列表的数据结构是`std::vector`,它也提供了动态数组的功能[^1]。然而,两者之间仍然存在一些差异。 #### 1. 动态数组特性 Python列表可以动态调整大小,添加或删除元素时无需手动管理内存。类似地,C++中的`std::vector`也支持动态扩展,但需要显式调用`push_back()`、`pop_back()`等方法来操作元素[^3]。 示例代码对比: ```python # Python列表操作 a = [1, 2, 3] a.append(4) # 动态添加元素 print(a) # 输出: [1, 2, 3, 4] ``` ```cpp // C++ std::vector 操作 #include <vector> #include <iostream> using namespace std; int main() { vector<int> a = {1, 2, 3}; a.push_back(4); // 动态添加元素 for (auto &x : a) cout << x << " "; // 输出: 1 2 3 4 return 0; } ``` #### 2. 类型约束 Python列表可以存储不同类型的数据(如整数、字符串等),而C++的`std::vector`是一个模板类,要求所有元素具有相同的类型。如果需要存储不同类型的数据,C++通常使用`std::variant`或`std::any`结合`std::vector`实现[^4]。 示例代码对比: ```python # Python列表存储不同类型 mixed_list = [1, "hello", 3.14] print(mixed_list) # 输出: [1, "hello", 3.14] ``` ```cpp // C++使用std::variant存储不同类型 #include <vector> #include <variant> #include <string> #include <iostream> using namespace std; int main() { vector<variant<int, string, double>> mixed_vector = {1, "hello", 3.14}; for (auto &x : mixed_vector) { if (holds_alternative<int>(x)) cout << get<int>(x) << endl; else if (holds_alternative<string>(x)) cout << get<string>(x) << endl; else if (holds_alternative<double>(x)) cout << get<double>(x) << endl; } return 0; } ``` #### 3. 灵活性与便捷性 Python列表提供了丰富的内置方法,如`append()`、`insert()`、`remove()`等,使得操作更加直观。C++中的`std::vector`虽然也有类似的方法,但在语法上相对繁琐[^2]。 示例代码对比: ```python # Python列表灵活操作 lesson = [&#39;maths&#39;, &#39;Chinese&#39;, &#39;English&#39;] lesson[0] = &#39;Biology&#39; # 修改元素 lesson.append(&#39;Physics&#39;) # 添加元素 lesson.insert(1, &#39;History&#39;) # 插入元素 del lesson[2] # 删除元素 print(lesson) # 输出: [&#39;Biology&#39;, &#39;History&#39;, &#39;Physics&#39;] ``` ```cpp // C++ std::vector 灵活操作 #include <vector> #include <iostream> using namespace std; int main() { vector<string> lesson = {"maths", "Chinese", "English"}; lesson[0] = "Biology"; // 修改元素 lesson.push_back("Physics"); // 添加元素 lesson.insert(lesson.begin() + 1, "History"); // 插入元素 lesson.erase(lesson.begin() + 2); // 删除元素 for (auto &x : lesson) cout << x << " "; // 输出: Biology History Physics return 0; } ``` #### 4. 内存管理 Python列表的内存管理由解释器自动处理,开发者无需关心底层细节。而在C++中,`std::vector`的内存管理虽然也较为自动化,但仍然需要开发者注意容量(capacity)和大小(size)的区别[^3]。 ### 结论 总体而言,Python中的列表最接近于C++中的`std::vector`,但两者的灵活性和使用场景有所不同。Python列表更注重简洁性和易用性,而C++的`std::vector`则提供了更高的性能和对底层细节的控制能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值