美文网首页
Python 列表(list)

Python 列表(list)

作者: 嗨皮Jerry | 来源:发表于2017-04-08 18:24 被阅读94次

Python 有三个排序方法,正序sort,sorted ,反序 severse

反序方法:severse,正序sort  ,sorted 正序 没有reverse属性使用后保存原列表顺序,

tom = ['a','x','f','d','m']

tom1 = ['2','4','7','1']

tom.reverse()              >>>['m', 'd', 'f', 'x', 'a']

tom1.reverse()            >>>['7', '4', '2', '1']

tom1.sort(reverse=True)>>>['7', '4', '2', '1']

tom1.sort()                      >>>['1','2','4','7']

tom2 = ['a','c','b','d','e'] 

sorted使用方法:没有reverse属性;;;;使用后保存原列表顺序(tom2保持不变)

tom3 = sorted(tom2)    >>>['a','b','c','d,'e']

print tom2  >>>['a','c','b','d','e']


修改删除列表元素,添加元素方法:extend、insert、append 三个方法

tom = ['a','x','f','d','m']

tom1 = ['2','4','7','1']

insert是具体在列表(list)某个位置添加方法如下:

tom1.insert(2,"z") >>>['2', '4', 'z', '7', '1']

extend可以在列表末尾添加多个元素:

tom.extend([1,23,5]) >>>['a', 'x', 'f', 'd', 'm', 1, 23, 5]

append只能将一个元素添加到列表的末尾(如果添加一个list,会将这个元素以一个列表 的形式添加进去):

tom1.append("one")>>>tom1.append("one")

tom1.append(['a','b','c'])>>>['a', 'x', 'f', 'd', 'm', 1, 23, 5,['a','b','c']]

删除元素的方法有:pop del remove 三个方法:

tom = ['a','x','f','d','m']

tom1 = ['2','4','7','1']

pop方法 删除列表末尾的一个元素

tom.pop()  >>>['a','x','f','d']

del准许用索引将元素删除

del tom1[1]>>>['2','4','7]

remove会从列表中选择你的一个元素删除

tom.remove('f')>>>['a','x','d','m']


搜索列表:查找元素是否在列表中(in);查找元素在列表中的索引  index

tom = ['a','x','f','d','m']

tom1 = ['2','4','7','1']

if "a" in tom:print `````else:print ```` 返回值 是 Ture 和False

print tom1.index(’3‘) >>>1


复制列表或者说建立一个列表的副本:要使用分片记法[:]

new = original[:]可以建立副本


双重列表:

tom = ['a','x','f','d','m']

tom1 = ['2','4','7','1']

new = [tom,tom1]>>>[['a','x','f','d','m'],['2','4','7','1']] 双重列表

print(new[1][2])>>>7

这里我想到了数据结构,应该要看看书了


相关文章

  • python list与set的区别

    python list与set的区别 python数据类型:列表List, Set集合; 列表List: 1.创...

  • 第四天学习python总结

    python的基本类型 一、List(列表)类型 List(列表) 是 Python 中使用最频繁的数据类型。列表...

  • python 列表

    python 列表 list: 定义: 变量=list() #定义空列表 变量=[] list的操作: 增加数...

  • Day01自学 - Python 列表(List)

    学习参考博客地址:Python 列表(List) | python list(列表)操作用法总结 一、基础语法 创...

  • (三)Python的List和Tuple类型

    Python创建List Python创建list Python内置的一种数据类型是列表:list。list是一种...

  • Python完结篇_基础知识2

    Python数列 1. 函数 len(list)—列表元素个数 max(list)—列表最大值 min(list...

  • 06 list列表和相关运算

    ====== list列表 ====== 1. 什么是列表(list) 列表是Python提供的容器型数据类型,可...

  • python set集合,排序输出

    python set集合,排序输出 列表-->集合-->列表-->list sort() == 集合-->列表--...

  • python_列表

    python 列表:list 列表:可以存储一组数据的类型;组合数据类型 创建列表 name=list() ...

  • Python list方法使用详解

    Python列表(list)的相关操作及方法 一、list列表 1.概述: 本质:list列表的本质是一种有序的集...

网友评论

      本文标题:Python 列表(list)

      本文链接:https://www.haomeiwen.com/subject/zudlattx.html