name_list = ["liming","xiaohong",15,{"hobby":"basketball"}]
name_list = []
通过索引值来访问列表中的元素,索引从0开始
name_list = ["liming","xiaohong",15,{"hobby":"basketball"}]
name_list[0]
#结果拿到的是liming
获取列表中的长度,内置函数len()
name_list = ["liming","xiaohong",15,{"hobby":"basketball"}]
name_list_len = len(name_list)
print(name_list_len)
#结果为4
name_list = ["liming","xiaohong",15,{"hobby":"basketball"}]
name_list[0] = "mss"
print(name_list)
#结果为["mss","xiaohong",15,{"hobby":"basketball"}]
说明:列表的切片于步长原理一模一样
列表名[start:end:step]
name_list = ["liming","xiaohong",15,{"hobby":"basketball"}]
print(name_list[1:3:2])
#结果["xiaohong"]
name_list[] print(dir(name_list[])
name_list = ["liming","xiaohong",15,{"hobby":"basketball"}]
name_list.append("ms")
print(name_list)
name_list = ["liming","xiaohong",15,{"hobby":"basketball"}]
name_list.clear()
print(name_list)
#结果为空
name_list = ["liming","xiaohong",15,{"hobby":"basketball"}]
print(name_list.count(15))
#结果为1
name_list1 = ["liming"]
name_list2 = ["xiaohong"]
name_list1.extend(name_list2)
print(name_list1)
语法:index(“查找的元素”,start,end) start 默认值为0,end默认值-1 且包含-1
name_list1 = ["liming"]
print(name_list1.index("liming")) #结果为0
语法:insert(index,插入的元素) 不写index会报错,index代表索引位置,插入在索引位置之前,如果想插入到最后一位,建议用append()方法
name_list1 = ["liming","xiaohong"]
name_list1.insert(1,"ms")
print(name_list1)
name_list = ["liming","xiaohong"]
pop_str = name_list.pop(0)
print(pop_str) #被删掉的依然可以打印
print(name_list) #结果liming确实被删掉了
name_list = ["liming","xiaohong"]
re_str = name_list.remove("liming")
print(re_str) #结果为空
print(name_list) #liming被删掉了
name_list = ["liming","xiaohong",15,{"hobby":"basketball"}]
name_list.reverse()
print(name_list)
name_list = ["liming","xiaohong"]
name_list.sort()
print(name_list)
str:字母顺序
int 从小到大