day 05-09 列表内置方法

 列表,元组
        查
            索引(下标) ,都是从0开始
            切片
            .count 查某个元素的出现次数
            .index 根据内容找其对应的位置
        print(a.count("haidilaoge"))
"haidilao ge" in a #需要执行打印 增加 a.append() 追加 a.insert(index, "内容") a.extend 扩展 修改 a[index] = "新的值" a[start:end] = [a,b,c] 删除 remove("内容") pop(index) #pop是有一个返回值的 pop()不加,自动默认为删除下标-1的最后一个) del a, del a[index] a.clear() 清空 排序 sort () reverse() 身份判断 >>> type(a) is list True >>>
#count
a = ['to','be','to','be','to'].count('be')
print(a)
print(a.count('be'))

#extend
a = ['1','2','3']
b = ['4','5','6']
a.extend(b)
print(a)  
print(b)
# a的值变为['1','2','3','4','5','6']
# b的值不变['4','5','6']
# 若不改变a或者b的值,又需要得到两者的组合用a+b
print(a+b)
 

 

#index  根据内容取位置
a = ['zhangsan','lisi','lixiaolong','wangwu','lixiaolong','shitailong','lixiaolong']
print(a.index('lixiaolong'))  #如果有多个,系统自动默认为第一个
如果要强行取第二个lixiaolong:

first_lixiaolong_index = a.index('lixiaolong')
print("first_lixiaolong_index",first_lixiaolong_index)
little_list = a[first_lixiaolong_index+1:]
second_liaolong_index = little_list .index('lixiaolong')
print("second_liaolong_index",second_liaolong_index)
second_lixiaolong_index_in_big_list = first_lixiaolong_index + second_liaolong_index +1
print("second_lixiaolong_index_in_big_list",second_lixiaolong_index_in_big_list)
    #验证
print("second_lixiaolong:",a[second_lixiaolong_index_in_big_list ])

#reverse
a = ['zhangsan','lisi','wangwu','lixiaolong','shitailong']
a.reverse()
print(a)

#sort
x =['1','4','7','9','5']
x.sort()   #按数字大小
print(x)
x.reverse()
print(x)
等价于:
x.sort(reverse=True)   #ctrl+鼠标点击
print(x)

a = ['zhangsan','Lisi','Wangwu','lixiaolong','shitailong']  #按ASCII编码顺序
a.sort()
print(a)
等价于:
b = sorted(a)
print(b)

print(type(a) is list)
print("zhangsan" in a)
print(a.count("zhangsan"))

 

posted @ 2017-12-03 12:18  minkillmax  阅读(133)  评论(0编辑  收藏  举报