标签:python列表
例子
name_list = ["Jin","Tom","Cat","Jak"]
索引(从0开始)
name_list(0)
结果:Jin
---------------
查看列表的有什么方法
dir(name_list)
append(追加)
name_list.append("Jin")
clear(清空)
name_list.cler()
cocount(统计)
name_list.cocount("Jin")
extend(扩展)
age_list = ["22","33","44","55"]
age_list.extend(age_list)
index(查找索引,只会返回查找到第一个)
age_list.index("jin")
insert(插入)
name_list.insert(2,jin)
name_list = ["Jin","Tom","jin","Cat","Jak"]
pop(删除一个,会删除最后一个)
name_list.pop
remove(删除指定一个)
name_list.remove("jin")
reverse(反转)
name_list.reverse()
sort(排序)
name_list.sort()
-------------------------------
列表切片
>>> name_list
[‘Jin‘, ‘Tom‘, 33, 33, ‘Cat‘, ‘Jak‘, ‘jak‘, ‘12‘, ‘33‘, ‘44‘, ‘55‘, ‘66‘]
>>> name_list[0:3] (0:3 这是索引值,切片会顾头不顾尾)
[‘Jin‘, ‘Tom‘, 33]
>>> name_list[4:]
[‘Cat‘, ‘Jak‘, ‘jak‘, ‘12‘, ‘33‘, ‘44‘, ‘55‘, ‘66‘]
>>> name_list[:-1]
[‘Jin‘, ‘Tom‘, 33, 33, ‘Cat‘, ‘Jak‘, ‘jak‘, ‘12‘, ‘33‘, ‘44‘, ‘55‘]
>>> name_list
[‘Jin‘, ‘Tom‘, 33, 33, ‘Cat‘, ‘Jak‘, ‘jak‘, ‘12‘, ‘33‘, ‘44‘, ‘55‘, ‘66‘]
>>> name_list[:-2]
[‘Jin‘, ‘Tom‘, 33, 33, ‘Cat‘, ‘Jak‘, ‘jak‘, ‘12‘, ‘33‘, ‘44‘]
>>> name_list[-3:-2]
[‘44‘]
本文出自 “半夏轻浅的博客” 博客,请务必保留此出处http://sublime.blog.51cto.com/8856101/1829680
标签:python列表
原文地址:http://sublime.blog.51cto.com/8856101/1829680