新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python列表數(shù)據(jù)如何增加和刪除
1、使用append函數(shù)來為列表 list 添加數(shù)據(jù),默認將數(shù)據(jù)追加在末尾。

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:python列表list.py
@Time:2021/3/22 00:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!
"""
list1 = list() #定義一個空列表
print("list1 : ",list1)
list1.append("hello") # 在列表list的末尾添加字符串 'hello'
list1.append(True) # 在列表list的末尾添加布爾值 True
print("list1 : ",list1)
'''
輸出結(jié)果:
list1 : []
list1 : ['hello', True]
'''2、列表中的數(shù)據(jù)從左到右,索引值默認從0開始以此遞增,和字符串的索引值類似。
刪除使用 del 關(guān)鍵字,直接列表 List 時根據(jù)數(shù)據(jù)對應(yīng)的索引值直接刪除。
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:python列表list.py
@Time:2021/3/22 00:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!
"""
list2 = [1,2,3,4,5,6,7,False,"python"]
print("刪除數(shù)據(jù)之前:{}".format(list2))
del list2[0] # 刪除列表中的(索引值等于0)第一個數(shù)據(jù),此時list2 中數(shù)據(jù)為[2,3,4,5,6,7,False,"python"]
print("第一次數(shù)據(jù)之后:{}".format(list2))
del list2[0] # 基于上一次的結(jié)果,刪除(索引值等于0)第一個數(shù)據(jù),此時list2 中數(shù)據(jù)為[3,4,5,6,7,False,"python"]
print("第二次數(shù)據(jù)之后:{}".format(list2))
del list2[3] # 基于上一次的結(jié)果,刪除(索引值等于3)第四個數(shù)據(jù),此時list2 中數(shù)據(jù)為[3,4,5,7,False,"python"]
print("第三次數(shù)據(jù)之后:{}".format(list2))
'''
輸出結(jié)果:
刪除數(shù)據(jù)之前:[1, 2, 3, 4, 5, 6, 7, False, 'python']
第一次數(shù)據(jù)之后:[2, 3, 4, 5, 6, 7, False, 'python']
第二次數(shù)據(jù)之后:[3, 4, 5, 6, 7, False, 'python']
第三次數(shù)據(jù)之后:[3, 4, 5, 7, False, 'python']
'''以上就是python列表數(shù)據(jù)的增加和刪除,希望對大家有所幫助。更多Python學習指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
名稱欄目:創(chuàng)新互聯(lián)Python教程:python列表數(shù)據(jù)如何增加和刪除
網(wǎng)頁網(wǎng)址:http://fisionsoft.com.cn/article/cooedjo.html


咨詢
建站咨詢
