l = ['a','b','c','d',1,2,[3,'e',4]]

1.list.append()

在list的结尾新增一个新的元素,没有返回值,但会修改原列表

l.append(5)
print(l)

Output:

['a', 'b', 'c', 'd', 1, 2, [3, 'e', 4], 5]

2.list.insert()

list.insert(index,value) 在指定的index插入一个新的元素

l.insert(3,'ha1')
print(l)

Output:

['a', 'b', 'c', 'ha1', 'd', 1, 2, [3, 'e', 4]]

3.list.extend()

括号里的值必须是可迭代元素(字符串、串列、元祖、集合) 将括号内的元素逐一加到列表的最后

l.extend('abcd')                #['a', 'b', 'c', 'd', 1, 2, [3, 'e', 4], 'a', 'b', 'c', 'd']
l.extend([123,2,'abc']) #['a', 'b', 'c', 'd', 1, 2, [3, 'e', 4], 123, 2, 'abc']
l.extend((1,2,[1,2,'abc'])) #['a', 'b', 'c', 'd', 1, 2, [3, 'e', 4], 1, 2, [1, 2, 'abc']]

4.list.pop()

list.pop(index = -1)  弹出列表中的一个元素,这里的默认元素为列表最后一个元素,并在结果返回元素值

l.pop()    #['a', 'b', 'c', 'd', 1, 2]
l.pop(2) #['a', 'b', 'd', 1, 2, [3, 'e', 4]]

5.list.remove()

list.remove()  无返回值,删除元素的第一个匹配值

l.remove('a')   #['b', 'c', 'd', 1, 2, [3, 'e', 4]]

6.list.clear()

用于清空列表

l.clear()   #[]

7.del

del l[2]    #['a', 'b', 'd', 1, 2, [3, 'e', 4]]
del l[2:] #['a', 'b']
del l[:-2] #[2, [3, 'e', 4]]
del l[::] #[]
del l[::-2] #['b', 'd', 2]
del l[::3] #['b', 'c', 1, 2]

8.改值

<1>按照索引改值

l[6][0] = 'g'    #['a', 'b', 'c', 'd', 1, 2, ['g', 'e', 4]]

<2>切片改值(逐一增加)

l[6][0:2] = 'abcd'    #['a', 'b', 'c', 'd', 1, 2, ['a', 'b', 'c', 'd', 4]]

<3>按照切片间隔改值遵循一一对应原则

l[6][::] = 'abc'    #['a', 'b', 'abcd', 'd', 1, 2, ['a', 'b', 'c']]
l[::2] = 'abcd' #['a', 'b', 'b', 'd', 'c', 2, 'd']

9.查值

通过切片或者for循环查值

10.公共方法

<1>len()

  计算列表的长度大小

print(len(l))    #

<2>list.count()

  统计列表中该元素出现的个数

print(l.count(2))    #

<3>list.index()

  list.index(substr,start,end)   获取该字符串在原字符串内的索引值,若找到则返回索引值,若未找到则返回错误 

index_val = l.index("c",1,5)    #
print(index_val)

11.排序

<1>list.sort()

  正向排序:

l = [1,5,6,8,9,10,2]
l.sort() #[1, 2, 5, 6, 8, 9, 10]
print(l)

  逆向排序:

l = [1,5,6,8,9,10,2]
l.sort(reverse = True) #[10, 9, 8, 6, 5, 2, 1]
print(l)

<2>list.reverse()

  将列表的元素翻转:

l = [1,5,6,8,9,10,2]
l.reverse() #[2, 10, 9, 8, 6, 5, 1]
print(l)

 

Python学习日记(二) list操作的更多相关文章

  1. Python学习日记(二十八) hashlib模块、configparse模块、logging模块

    hashlib模块 主要提供字符加密算法功能,如md5.sha1.sha224.sha512.sha384等,这里的加密算法称为摘要算法.什么是摘要算法?它又称为哈希算法.散列算法,它通过一个函数把任 ...

  2. Python学习日记:day8-------文件操作

    文件操作 1,文件路径:d:\xxxx.txt     绝对路径:从根目录到最后     相对路径:当前目录下的文件 2,编码方式:utf-8 3,操作方式:只读,只写,追加,读写,写读...... ...

  3. Python学习日记(二十六) 封装和几个装饰器函数

    封装 广义上的封装,它其实是一种面向对象的思想,它能够保护代码;狭义上的封装是面向对象三大特性之一,能把属性和方法都藏起来不让人看见 私有属性 私有属性表示方式即在一个属性名前加上两个双下划线 cla ...

  4. Python学习日记(二十三) 类命名空间和组合

    类命名空间 在一个类中它的函数(方法)属于动态属性,直接定义的变量属于静态属性 首先先定义一个类,并在这个类里面加入静态变量.属性等然后将一个对象实例化 class Fighter: #定义一个战机的 ...

  5. Python学习日记(二十一) 异常处理

    程序中异常的类型 BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是输入^C) Exception 常规错误的基 ...

  6. python学习日记(文件操作练习题)

    登录注册(三次机会) name = input('请注册姓名:') password = input('请注册密码:') with open('log',mode='w',encoding='utf- ...

  7. Python学习日记(二十七) 反射和几个内置函数

    isinstance() 判断isinstance(obj,cls)中obj是否是cls类的对象 class Person: def __init__(self,name): self.name = ...

  8. Python学习日记(二十五) 接口类、抽象类、多态

    接口类 继承有两种用途:继承基类的方法,并且做出自己的改变或扩展(代码重用)和声明某个子类兼容于某基类,定义一个接口类interface,接口类中定义了一些接口名(就是函数名)且并未实现接口的功能,子 ...

  9. Python学习日记(二十四) 继承

    继承 什么是继承?就是一个派生类(derived class)继承基类(base class)的字段和方法.一个类可以被多个类继承;在python中,一个类可以继承多个类. 父类可以称为基类和超类,而 ...

随机推荐

  1. vue使用formData进行文件上传

    本文为博主原创,未经允许不得转载 1.vue页面 <ux-form ref="formRef" layout="vertical"> <ux- ...

  2. mongo 笔记

    mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options ...

  3. GSON工具类

    import java.util.Map; import com.google.gson.reflect.TypeToken; import com.google.gson.FieldNamingPo ...

  4. Cannot start service WMSvc on computer '.'.

    批处理,管理员权限执形 taskkill /im wmsvc.exe /f net stop WMSVC net start WMSVC pause

  5. Javascript 将 console.log 日志打印到 html 页面中

    如何将 console.log() 打印的日志输出到 html 页面中 (function () { var old = console.log; var logger = document.getE ...

  6. mybatis:字符串转成数组拼接成SQL

    <foreach item="item" index="index" collection="str.split(',')" open ...

  7. jenkins中启用tag标签

    参照里面的第9步: https://www.cnblogs.com/effortsing/p/10468840.html

  8. hihocoder 1566 皇室成员的名字

    #1566 : 皇室成员的名字 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho正在学习世界历史.他发现历史上很多西方国家的皇室成员的名字都是由英文名字加罗马数字组 ...

  9. Get Docker Engine - Community for Ubuntu

    Get Docker Engine - Community for Ubuntu Uninstall old versions$ sudo apt-get remove docker docker-e ...

  10. 【转帖】处理器的三国时代:DR公司盛气凌人,IBM转身成就微软

    处理器的三国时代:DR公司盛气凌人,IBM转身成就微软 https://www.eefocus.com/mcu-dsp/360555 <处理器史话>之五 2016-04-06 15:24  ...