切片:动手试一试
1.切片打印
testlist=['cos','adi','momo','toka','wwc','qqc'] #打印前三个元素 print('The first three items in the list are:',testlist[0:3]) #打印中间三个元素 print('Three items from the middle of the list are:',testlist[1:4]) #打印后三个元素 print('The last three items in the list are:',testlist[-3:])
2.复制副本和循环打印
testlist=['cos','adi','momo','toka','wwc','qqc'] testlistcopy=testlist[:]#直接写列表名代表指向的复制,而不是复制创建副本 testlist.append('123321') testlistcopy.append(99999) for a in testlist: print(a) for b in testlistcopy: print(b)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步