Python实战之列表简单练习
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] names = ['jone','1','xiaoming','zhangsan','lisi','wangermazi','william'] print(names) #打印整个列表 #['jone', '1', 'xiaoming', 'zhangsan', 'lisi', 'wangermazi', 'william'] print("names[0]:",names[0]) print("names[-1]:",names[-1]) # 按列表下表取值,从最后往前数,分别是-1,-2... # names[0]: jone # names[-1]: william print(names[1:4]) print(names[0:7:3]) # 列表切片,取多个元素,注意顾头不顾尾。其中3是步长。 # ['1', 'xiaoming', 'zhangsan'] # ['jone', 'zhangsan', 'william'] names.append("xiaotaoqi") print(names) # ['jone', '1', 'xiaoming', 'zhangsan', 'lisi', 'wangermazi', 'william', 'xiaotaoqi'] # append 追加到列表最后面,与insert对比学习. names = ['jone','1','xiaoming','zhangsan','lisi','wangermazi','william'] names.insert(0,'xiaoshagua') print(names) # ['xiaoshagua', 'jone', '1', 'xiaoming', 'zhangsan', 'lisi', 'wangermazi', 'william'] names.insert(3,'123') print(names) # ['xiaoshagua', 'jone', '1', '123', 'xiaoming', 'zhangsan', 'lisi', 'wangermazi', 'william'] names.insert(-1,'ershagua') print(names) # ['xiaoshagua', 'jone', '1', 'xiaoming', 'zhangsan', 'lisi', 'wangermazi', 'ershagua', 'william'] names = ['jone','1','xiaoming','zhangsan','lisi','wangermazi','william'] names[0] = 'new_jone' print(names) # 修改单个值 # ['new_jone', '1', 'xiaoming', 'zhangsan', 'lisi', 'wangermazi', 'william'] names = ['jone','william','xiaoming','zhangsan','lisi','wangermazi','william'] names.sort() print(names) # 排序 # ['jone', 'lisi', 'wangermazi', 'william', 'william', 'xiaoming', 'zhangsan'] names.reverse() print(names) #反转 # ['zhangsan', 'xiaoming', 'william', 'william', 'wangermazi', 'lisi', 'jone'] names = ['jone','william','xiaoming','zhangsan','lisi','wangermazi','william'] print(names.index("lisi")) #获取下标,index # 4 #删除三法:pop,remove,del names = ['jone','1','xiaoming','zhangsan','lisi','wangermazi','william'] print(names) # ['jone', '1', 'xiaoming', 'zhangsan', 'lisi', 'wangermazi', 'william'] del names[2] print(names) # ['jone', '1', 'zhangsan', 'lisi', 'wangermazi', 'william'] names.remove('lisi') print(names) # ['jone', '1', 'zhangsan', 'wangermazi', 'william'] names.pop() print(names) # ['jone', '1', 'zhangsan', 'wangermazi'] names.pop(2) print(names) # ['jone', '1', 'wangermazi'] names = ['jone','1','xiaoming','zhangsan','lisi','wangermazi','william'] names2 = ['xiaotaoqi','ershan'] names.extend(names2) print(names) #拓展 # ['jone', '1', 'xiaoming', 'zhangsan', 'lisi', 'wangermazi', 'william', 'xiaotaoqi', 'ershan'] names = ['jone','william','1','xiaoming','zhangsan','lisi','wangermazi','william'] #统计某个元素出现的个数 print(names.count('william')) #2 names = ['jone','1','xiaoming','zhangsan','lisi','wangermazi','william'] #copy new_names = names.copy() print(names) # ['jone', '1', 'xiaoming', 'zhangsan', 'lisi', 'wangermazi', 'william'] print(new_names) # ['jone', '1', 'xiaoming', 'zhangsan', 'lisi', 'wangermazi', 'william'] print(names==new_names) # True
posted on 2017-06-23 10:54 William126 阅读(219) 评论(0) 编辑 收藏 举报