python 列表的常用方法

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#list 列表的方法
ww_lst=['2018-12-3','周一']
ww_lst.append('圣诞节前') #追加 增
ww_lst.insert(5,'倒计时开始') #在指定位置前面,插入这个串 ['2018-12-3', '周一', '圣诞节前', 1, 4, '倒计时开始', 6, 8]
temp=(1,4,6,8)
ww_lst.extend(temp) #将extend 中指定的迭代对象追加到list中 ['2018-12-3', '周一', '圣诞节前', 1, 4, 6, 8]

copy_lst=ww_lst.copy()
#print(ww_lst.count('周一')) #统计指定串,出现的次数
#print(ww_lst.index(6)) #index,返回指定串所在的索引序号

ww_lst.pop(4) #根据索引序号删除对象,default last ['2018-12-3', '周一', '圣诞节前', 1, '倒计时开始', 6, 8]
ww_lst.remove('倒计时开始') #根据对象内容删除 ['2018-12-3', '周一', '圣诞节前', 1, 6, 8]

#ww_lst.reverse() #反转 [8, 6, 1, '圣诞节前', '周一', '2018-12-3']
print(ww_lst) #ww_lst 中数据类型有多种,不能使用sort排序
temlst=[1,3,6,2,5,8]
temlst.sort() #排序
print(temlst)

#print(ww_lst[0:3]) #ww_lst[:3] 效果是一样的,切片N:M,不包含M 当N为起始值可以省略
#print(ww_lst[:])#同样,M为最后一个值可以省略
#print(ww_lst[0:-1])#跟[:]效果不一样,[0:-1]是不包含-1的。
#print(ww_lst[::2])#切片带步长

#元组的操作
wwtuple=('籍贯','住址')
#print(wwtuple.count('住址')) #统计指定串 出现的次数
print(wwtuple.index('住址'))#显示指定串的索引序号
posted @ 2018-12-25 16:30  幸福在今天  阅读(196)  评论(0)    收藏  举报