本篇文章给大家谈谈python列表功能默写定义空列表lt,以及python列表有哪些基本操作,希望对各位有所帮助,不要忘了收藏本站喔。
Source code download: 本文相关源码
Python list是一种有序的可变序列,可以存储任意数量的元素,每个元素可以是任何类型的数据。本文将介绍Python list的特性、作用、底层原理和使用方法python流星雨代码。
我们可以使用方括号[]或list()函数来创建一个list,使用方括号和索引来访问list中的元素,使用append()、extend()和insert()方法向list中添加元素,使用del、remove()和pop()方法删除list中的元素。
特性
Python list的特性如下:
1. 可变性:list是一种可变的数据类型,可以在运行时添加、删除或修改元素。
2. 有序性:list中的元素是有序的,可以通过索引访问它们。
3. 可重复性:list中的元素可以重复,可以包含任意数量的相同元素。
4. 可嵌套性:list可以嵌套在其他list中,也可以嵌套在其他数据类型中。
作用
Python list的作用如下:
1. 存储数据:list可以用于存储任何类型的数据,包括数字、字符串、列表、元组、字典等。
2. 快速访问数据:由于list中的元素是有序的,因此可以通过索引快速访问数据,时间复杂度为O(1)。
3. 处理结构化数据:list可以用于处理结构化数据,例如CSV格式的数据。
底层原理
Python list的底层原理是使用动态数组实现的。动态数组是一种数组类型,它可以在运行时动态分配内存空间,以容纳不断增长的元素。当数组的容量不足时,动态数组会自动分配更多的内存空间,以容纳更多的元素。由于动态数组的实现,Python list可以快速访问数据,时间复杂度为O(1)。
使用
Python list的使用方法如下:
1. 创建list
使用方括号[]或list()函数可以创建一个空list,例如:
empty_list = []
empty_list = list()
# 创建一个非空的list
my_list = [1, 2, 3, 'hello', True]
也可以在创建list时指定元素,例如:
from inner_datastruct import WEIXIN_URL, CSDN_URL, GIT_URL
url = [WEIXIN_URL, CSDN_URL, GIT_URL]
2. 访问list
可以使用索引来访问list中的元素,例如:
print(url[0]) # https://mp.weixin.qq.com/s/0yqGBPbOI6QxHqK17WxU8Q
# 访问最后一个元素
print(url[-1])
如果索引超出了list的范围,则会引发IndexError异常,可以使用len()函数来获取list的长度,例如:
print(len(url)) # 3
3. 修改list
可以使用索引来修改list中的元素,例如:
url[1] = 'www.baidu.com'
如果索引超出了list的范围,则会引发IndexError异常。
4. 添加元素
我们可以使用append()方法向list中添加一个元素,使用extend()方法向list中添加多个元素,使用insert()方法在指定位置插入一个元素。
使用append()方法向list末尾添加一个元素,例如:
url.append(CSDN_URL)
也可以使用extend()方法向list末尾添加多个元素,合并两个列表的元素,例如:
numbers = [1, 2, 3]
print(numbers) # [1, 2, 3]
numbers.extend([11, 22, 33])
print(numbers) # [1, 2, 3, 11, 22, 33]
还可以使用insert()方法在指定位置添加一个元素,例如:
numbers.insert(0, 0)
print(numbers) # [0, 1, 2, 3, 11, 22, 33]
my_list = [1, 2, 3, 'hello', True]
# 添加一个元素
my_list.append('world')
# 添加多个元素
my_list.extend([4, 5, 6])
# 在第二个位置插入一个元素
my_list.insert(1, 'python')
print(my_list)
5. 删除元素
我们可以使用del语句删除list中的一个元素,使用remove()方法删除指定的元素,使用pop()方法删除指定位置的元素。
可以使用del语句删除list中的元素,例如:
del url[1]
print(url) # ['https://mp.weixin.qq.com/s/0yqGBPbOI6QxHqK17WxU8Q', 'https://gitee.com/SteveRocket/practice_python.git', 'https://blog.csdn.net/zhouruifu2015/']
也可以使用pop()方法删除list中的元素,并返回删除的元素,例如:
print(url.pop()) # https://blog.csdn.net/zhouruifu2015/
print(url) # ['https://mp.weixin.qq.com/s/0yqGBPbOI6QxHqK17WxU8Q', 'https://gitee.com/SteveRocket/practice_python.git']
my_list = [1, 'python', 2, 3, 'hello', True, 'world', 4, 5, 6]
# 删除第一个元素
del my_list[0]
# 删除最后一个元素
my_list.pop()
# 删除指定元素
my_list.remove('hello')
print(my_list) # ['python', 2, 3, True, 'world', 4, 5]
6. 遍历list
可以使用for循环遍历list中的元素,例如:
for number in numbers:
print(number)
也可以使用range()函数和索引遍历list中的元素,例如:
# list遍历
for i in range(len(url)):
print(url[i])
7. 演示与py2三处不一样的用法
checklist = ["Microsoft Windows 7", "CentOS Linux 3"]
# 遍历列表
# for i in xrange(len(checklist)): # py2
for i in range(len(checklist)): # TODO xrange在py3中已不适用
print(checklist[i], type(checklist[i]))
# 输出
# Microsoft Windows 7 <type 'str'>
# CentOS Linux 3 <type 'str'>
print("列表长度:", len(checklist)) # 列表长度: 2
# 列表过滤
list1 = [1, 2, 3, 4, 5, 6, 7, 8]
list2 = [1, 2, 3, 4]
# 求两个集合的并集(还可以使用交集方法来实现)
for l in list1:
if l not in list2:
list2.append(l)
print(list2) # [1, 2, 3, 4, 5, 6, 7, 8]
ll3 = [5, 6, 7, 8]
N = []
for i in list1:
if i not in ll3:
N.append(i)
print("N:", N) # N: [1, 2, 3, 4]
list3 = [1, 6, 2, 3, 15, 4, 3, 7, 8, 2, 1]
print(list3, len(list3))
# #列表排序,并去重
l = list(set(list3))
print(l, len(l))
# [1, 6, 2, 3, 15, 4, 3, 7, 8, 2, 1] 11
# [1, 2, 3, 4, 6, 7, 8, 15] 8
# 列表排序,不去重
list4 = [1, 6, 2, 3, 15, 4, 3, 7, 8, 2, 1]
print(list4) # [1, 6, 2, 3, 15, 4, 3, 7, 8, 2, 1]
list4.sort()
print(list4) # [1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 15]
# 列表不排序 去重
# list5 = [1, 4, 3, 3, 4, 2, 3, 4, 5, 6, 1, 8, 8, 8, 8, 8]
# func = lambda x, y: x if y in x else x + [y]
# print(func([], list5))
# print( "func=", reduce(func,[[],]+list5)) # TODO reduce在py3中无法直接这样使用
l = ["this is list"]
# 根据索引取出列表的值
print(l[0]) # this is list
print(l[0] == ["this is list"]) # False
print(l[0] == "this is list") # True
l6 = [11, 22, 44, 66, 77]
l7 = [3, 6, 8, 9, 777]
# 列表插入列表,此处不是合并,跟extend不一样
l6.append(l7)
print("l6:", l6) # l6: [11, 22, 44, 66, 77, [3, 6, 8, 9, 777]]
print("l7:", l7) # l7: [3, 6, 8, 9, 777]
# 列表合并
l8 = [999, 333, 666, 777]
print(l7 + l8) # [3, 6, 8, 9, 777, 999, 333, 666, 777]
List = [{"id": "26721"}, {"id": 27.300}, {"id": 28543}]
# 遍历列表 获取字典的值 获取列表元素类型
for i in List:
print(i, i.get("id"), type(i))
# {'id': '26721'} 26721 <type 'dict'>
# {'id': 27.3} 27.3 <type 'dict'>
# {'id': 28543} 28543 <type 'dict'>
N = []
N2 = []
M = []
M2 = []
M3 = []
List = [{"aaa": 111}, {"bbb": 222}, {"ccc": 333}]
for i in List:
N.append(i.values()) # i.values() 返回值是list
# N2.append(i.values()[0]) # TODO py2中的values返回的是值组成的列表类型,与py3不一样。取出列表中每个字典的值,存放到N2中
N2.append(list(i.values())[0]) # 取出列表中每个字典的值,存放到N2中
M += i # 将列表元素的键放到列表中去
M2.append(i) # 将每个键值对添加到M2中
M3 += i.keys() # 效果等价于操作M
print(i, i.values())
# py2
# {'aaa': 111} [111]
# {'bbb': 222} [222]
# {'ccc': 333} [333]
# py3
# {'aaa': 111} dict_values([111])
# {'bbb': 222} dict_values([222])
# {'ccc': 333} dict_values([333])
print(N)
# py2返回的结果:[[111], [222], [333]] py3返回的结果:[dict_values([111]), dict_values([222]), dict_values([333])]
总结
Python list是一种可变的、有序的序列,可以存储任意数量的元素,每个元素可以是任何类型的数据。list是Python编程中非常常用的数据结构,可以用于存储任何类型的数据,快速访问数据,处理结构化数据等。list的底层实现是使用动态数组,可以快速访问数据。在使用list时,可以创建、访问、修改、添加元素、删除元素、遍历list中的元素。
Python的list是一种强大的数据类型,它是一种有序、可变的序列,可以存储任意类型的数据。在Python中,list是最常用的数据类型之一,它可以用于存储一组数据,进行元素的添加、删除、修改等操作。
更多资料 · 微信公众号搜索【CTO Plus】关注后,获取更多,我们一起学习交流。
关于公众号的描述访问如下链接