Python基础(四):列表和元祖常用方法总结

是小鱼呀·2022-02-09 21:15·284 次阅读

Python基础(四):列表和元祖常用方法总结

列表方法

列表描述#

  • 列表是Python中最基本的数据结构,是最常用的Python数据类型,列表的数据项不需要具有相同的类型
  • 列表是一种有序的集合,序列都可以进行的操作包括索引,切片,加,乘,检查成员。
  • Python已经内置确定序列的长度以及确定最大和最小的元素的方法。
  • 与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等。

创建列表#

Copy
>>> list1 = ['python', 2018, 'python3', 1994] >>> list1 ['python', 2018, 'python3', 1994] >>> list2 = [1, 2, 3, 4] >>> list2 [1, 2, 3, 4] >>> list3 = ['a', 'b', 'c', 'd'] >>> list3 ['a', 'b', 'c', 'd']

获取列表元素个数(长度)#

Copy
>>> list1 = ['python', 2018, 'python3', 1994] >>> len(list1) 4

访问列表中的值#

  1. 使用索引来访问列表中的值,列表的索引从0开始
Copy
>>> list1 = ['python', 2018, 'python3', 1994] >>> list1[0] 'python'

注意:当索引超出范围时,Python会报一个IndexError错误,所以,要确保索引不要越界,记得最后一个元素的索引是len(list1) - 1。
2. 获取最后一个元素

Copy
>>> list1[-1] 1994
  1. 切片
  • 使用索引从列表中获取多个元素,叫列表分片
    截取列表前3个元素:
Copy
>>> list1 = ['python', 2018, 'python3', 1994] >>> list1[0:3] ['python', 2018, 'python3'] >>> list1[:3] #如果第一个索引是0,可以省略 ['python', 2018, 'python3']

倒数切片:

Copy
>>> list1[-2:] #获取后2个元素 ['python3', 1994] >>> list1[-2:-1] # 获取倒数第二个元素 ['python3']

前4个元素,每两个取一个:

Copy
>>> list1[:4:2] ['python', 'python3']

所有元素,每3个取一个:

Copy
>>> list1[::3] ['python', 1994]

原样复制一个列表:

Copy
>>> list1[:] ['python', 2018, 'python3', 1994]

合并列表#

Copy
>>> list1 = ['python', 2018, 'python3', 1994] >>> list2 = [1, 2, 3, 4] >>> list4 = list1 + list2 >>> list4 ['python', 2018, 'python3', 1994,1, 2, 3, 4]

修改列表#

用索引来修改元素

Copy
>>> list1 ['python', 2018, 'python3', 1994] >>> list1[1] = 2017 >>> list1 ['python', 2017, 'python3', 1994]

删除列表元素#

  1. remove(): 从列表删除你选择的元素,你不需要知道这个元素的具体位置,只需要知道它是否在列表中,如果不在列表中会报错.
Copy
list=['python', 2018, 'python3', 1994] list.remove('python') print(list) [2018, 'python3', 1994]
  1. del: 利用索引从列表中删除元素
Copy
list=['python', 2018, 'python3', 1994] del list[0] print(list) [2018, 'python3', 1994]
  1. pop():括号里没有提供参数时,pop()会返回最后一个元素,并把它从列表中删除。
    如果在括号中放入一个参数,pop(N)会给出这个索引位置上的元素,并把它从列表中删除
    1) 从列表中删除最后一个元素。可以为最后一个元素指派一个名字
Copy
list=['python', 2018, 'python3', 1994] latlist=list.pop() print(latlist) print(list) 1994 ['python', 2018, 'python3']

2)通过索引删除

Copy
list=['python', 2018, 'python3', 1994] latlist=list.pop(1) print(latlist) print(list) 2018 ['python', 'python3', 1994]

搜索列表#

  1. in关键字:查找元素是否在列表中,如果在列表中会返回True,否则返回False
Copy
if 'a' in newList: print ('find a in newList') else : print ('do not find a in newList')
  1. index(): 查找元素在列表中的哪个位置(元素的索引),和remove()一样,如果没有找到这个元素会报错,最好和in结合使用
Copy
print (list.index('python')) 0

列表排序#

  1. sort() : 按字母顺序从小到大进行排序,数字同理,永久排序
Copy
newList=[1,3,4,2] newList.sort() print(newList) [1,2,3,4]
  1. reverse(): 按逆序排列.先按照正常的顺序排序,再reverse
    永久排序
Copy
newList.sort() newList.reverse() print(newList) [4,3,2,1]
  1. sort(reverse = True):增加参数,直接逆序排列,永久排序
Copy
newList.sort(reverse = Trueprint(newList) [4,3,2,1]
  1. sorted(list,reverse=True、False): 按顺序排列,不影响原来的列表,临时排序。True 倒序,False正序
  2. reversed(list):临时倒序
Copy
old = [5,4,3,2,1] new = sorted(old) print(old) print(new) [5,4,3,2,1] [1,2,3,4,5]

清空列表#

Copy
>>> list1 ['python', 2017, 'python3', 1994] >>> list1.clear() >>> list1 []

向列表增加元素#

  1. append(): 追加,在列表末尾增加一个元素。该方法无返回值,但是会修改原来的列表
Copy
list1 = ['Google', 'Runoob', 'Taobao'] list1.append('Baidu') print ("更新后的列表 : ", list1) 更新后的列表 : ['Google', 'Runoob', 'Taobao', 'Baidu']
  1. extend(): 向列表末尾增加多个元素,一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。
Copy
list1 = ['Google', 'Runoob', 'Taobao'] list2=list(range(5)) # 创建 0-4 的列表 list1.extend(list2) # 扩展列表 print ("扩展后的列表:", list1) 扩展后的列表: ['Google', 'Runoob', 'Taobao', 0, 1, 2, 3, 4]
  1. insert(index, obj): 在列表中的某个位置增加一个元素,不一定非要在列表末尾
Copy
list1 = ['Google', 'Runoob', 'Taobao'] list1.insert(1, 'Baidu') print ('列表插入元素后为 : ', list1) 列表插入元素后为 : ['Google', 'Baidu', 'Runoob', 'Taobao']

统计元素在列表中出现的次数#

list.count(obj): 返回元素在列表中出现的次数,一个都没有返回0

Copy
aList = [123, 'Google', 'Runoob', 'Taobao', 123]; print ("123 元素个数 : ", aList.count(123)) print ("Runoob 元素个数 : ", aList.count('Runoob')) 123 元素个数 : 2 Runoob 元素个数 : 1

浅拷贝和深拷贝#

  • copy.copy(li) 浅拷贝,对地址的拷贝,被拷贝的列表中可变类型改变,新的列表也会改变。不可变类型改变,新的列表不变
  • copy.deepcopy(li)深拷贝,对值的拷贝,产生新的列表与被拷贝的列表互不影响
  • li = old_li 赋值,两个变量都指向同一个内存块,修改li会对old_li产生影响,同理,修改old_li也会对li产生影响

数组遍历#

Copy
lists = [1, 2, 3, 4, 5] print("--------# 只遍历值------------") # 只遍历值 for i in lists: print(i) print("--------# 逆序遍历--1----------") # 逆序遍历 for i in lists[::-1]: print(i) print("--------# 逆序遍历--2----------") # 逆序遍历 for i in range(len(lists), 0, -1): print(i) print("--------# 遍历键和值--2----------") # 遍历键和值 for idx, val in enumerate(lists): print(idx,':',val) print("--------# 遍历键----------") # 只遍历键 for idx in range(0, len(lists)): print(idx)

元组方法

元组描述#

  • 元组与列表类似,但元组的元素不能修改
  • 元组使用小括号
Copy
>>>tup1 = ('Google', 'Runoob', 1997, 2000) >>> tup2 = (1, 2, 3, 4, 5 ) >>> tup3 = "a", "b", "c", "d" # 不需要括号也可以 >>> type(tup3) <class 'tuple'>

访问元组#

使用下标索引来访问元组中的值

Copy
tup1 = ('Google', 'Runoob', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", tup1[0]) print ("tup2[1:5]: ", tup2[1:5]) tup1[0]: Google tup2[1:5]: (2, 3, 4, 5)

合并元组#

Copy
>>> tup5 = tup2 + tup3 >>> tup5
posted @   是小鱼呀  阅读(284)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示
目录