# 列表:列表python中最基本的数据结构
# 序列中每个值都有对应的位置值
# python 有六个序列的内置类型,但是最常见的操作 索引 切片 加 乘 检查
# 列表是最常见的常用的python数据类型它可以作为一个方括号内的逗号分隔值出现
# 访问
list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print(list[0])
print(list[1])
print(list[2])
print("-------------------")
print(list[-1])
print(list[-2])
print(list[-3])
# 使用下标索引来访问列表中的值,同样你也可以使用方括号[]的形式截取字符
temp = list[0:4]
print(temp) # ['red', 'green', 'blue', 'yellow']
# 列表中使用的是[a:b] 形式
temp = list[1:-2]
print(temp)
# append 在列表末尾添加新对象
list2 = [1, 2, 3, 4, "a", "b", 3, 3, "a"]
print(list2)
list2.append('traditional')
print(list2)
# count(obj) 统计某个元素在列表中出现的次数
print(list2.count(3))
# index() 函数用于从列表中找出某个值第一个匹配项的索引位置
# 语法: index()方法语句
print("3的索引位置", str(list2.index("a")))
# insert(index,obj) 用于将指定对象插入列表的指定位置 没有返回值
list2 = ["google", "run", "sun"]
list2.insert(1, "Baidu")
print(list2)
# pop()函数 用于移除列表中的一个元素,(默认最后一个元素)。并且返回该元素的值
# pop()方法语法
list2 = ["google", "run", "sun", "play", "dance", "sing", "sang"]
print(list2.pop(3))
print(list2)
# remove() 函数用于移除列表中某个值的第一个匹配项
list2.remove("google")
print(list2)
# reverse() 反向列表中的元素
list2.reverse()
print(list2)
# sort 排序
arr = [1, 4, 2, 6, 2, 4, 2]
arr.sort(reverse=True)
print(arr)
ar = [1, 4, 2, 6, 2, 4, 2]
print(sorted(ar))