1.列表的增操作(四种)

  1. append(object):append object to end,directly used on list
  2. insert(index,object):insert object before index,directly used on list
  3. extend(iterable object):extend list by appending elements from the iterable,directly used on list
  4. "+":拼接,list1 + list2 = [each elements in list1 and list2]
# 1.append
a.append([9,8,7,6])
print(a)
--[1, 2, 3, 4, 5, 6, [9, 8, 7, 6]] # 2.insert
a.insert(7, 8)
print(a)
--[1, 2, 3, 4, 5, 6, [9, 8, 7, 6], 8] # 3. extend
a.extend("zhang")
print(a)
--[1, 2, 3, 4, 5, 6, [9, 8, 7, 6], 8, 'z', 'h', 'a', 'n', 'g'] # 4. +
a = a+[9]
print(a)
--[1, 2, 3, 4, 5, 6, [9, 8, 7, 6], 8, 'z', 'h', 'a', 'n', 'g', 9]

2.列表的删操作(四种)

  1. remove(value):remove first occurrence of value, Raises ValueError if the value is not present.directly used on list
  2. pop(index):remove and return item at index (default last),Raises IndexError if list is empty or index is out of range.directly used on list
  3. del[start:end:step]:remove items chosen by index,directly used on list
  4. clear():remove all items from list
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# 1.remove
a.remove(3)
print(a)
--[1, 2, 4, 5, 6, 7, 8, 9] # 2.pop
s = a.pop(1)
print(s)
print(a)
--2
--[1, 4, 5, 6, 7, 8, 9] # 3. del
del a[0:4:2]
print(a)
--[4, 6, 7, 8, 9] # 4. clear
a.clear()
print(a)
--[]

3.列表的改操作(两种)

  直接利用 list[index] = object 修改,[index]可以按照切片的格式修改多个,切片的部分规则如下

  1. 类似于replace(替换)方法,[ ]内选的值无论多少均删去,新修改的元素无论多少均插入list中
  2. 当新元素只是一个单独的字符串时,将字符串分解为单个字符后全部加入列表
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
a[1:2] = "", "", ""
print(a)
--[1, '', '', '', 3, 4, 5, 6, 7, 8, 9] a[1:3] = "", "", ""
print(a)
--[1, '', '', '', '', 3, 4, 5, 6, 7, 8, 9] a[1:4] = "", ""
print(a)
--[1, '', '', '', 3, 4, 5, 6, 7, 8, 9] a[1:2] = "come on"
print(a)
--[1, 'c', 'o', 'm', 'e', ' ', 'o', 'n', '', '', 3, 4, 5, 6, 7, 8, 9]

4.列表的查操作(两种)

  • directly query with list[index]
  • using for loop just like “for i in list ”,each i in loop is item in list.

5.count(value)

count(value):return number of occurrences of value

6.index(value)

return first index of value.Raises ValueError if the value is not present.

7.reverse()

reverse *IN PLACE*

8.sort(key=None, reverse=False)

stable sort *IN PLACE*, if reverse is True(default is False), sort from big to small.
												

python之列表操作的更多相关文章

  1. Python:列表操作总结

    一.创建一个列表 只要把逗号分隔的不同数据项使用方括号括起来即可 list1=['physics','chemistry',1997,2000] list2=[1,2,3,4,5,6,7] [注]:1 ...

  2. python之列表操作的几个函数

    Python中的列表是可变的,这是它却别于元组和字符串最重要的特点,元组和字符串的元素不可修改.列举一些常用的列表操作的函数和方法. 1,list.append(x),将x追加到列表list末尾: 1 ...

  3. python之列表操作(list)

    # 列表操作功能汇总 print("列表操作功能汇总") list_demo = ['first', 'second', 'thrid', 'fourth'] # 复制list_d ...

  4. 关于python的列表操作(一):取值,增加,修改,删除

    # 列表操作 name_list = ["wang", "niu", "bai", "sui"] # 取值 print( ...

  5. 关于python的列表操作(二):排序,统计

    # 列表操作 num_list = [2, 5, 8, 6, 7, 9, 5, 7] # 升序 num_list.sort() print(num_list) # 降序 num_list.sort(r ...

  6. Python中列表操作进阶及元组

    列表高级操作 一.遍历列表 >>> ls=['a','d','it'] >>> for val in ls: ... print (val) ... a d it ...

  7. Python中列表操作函数append的浅拷贝问题

    L=int(input())#L位数N=int(input())#N进制row=[]list1=[]for i in range(1,N): row.append(1)list1.append(row ...

  8. python基础-----列表操作

    在Python中用[]来表示列表,并用逗号隔开其中的元素. 1.访问列表元素 name=["zhangsan","lisi","ljy"] ...

  9. Python 之列表操作

    # len(list)列表元素个数 # max(list)返回列表元素最大值 # min(list)返回列表元素最小值 # list(seq)将元组转换为列表 # list.append(obj)在列 ...

随机推荐

  1. Pycharm出现的部分快捷键无效问题及解决办法

    为了进行python开发,下载了Pycharm.但是发现启动后,执行ctrl+c和ctrl+v等快捷键都无法生效. 网上搜索了下,参考https://blog.csdn.net/c2366994582 ...

  2. linux比较两个文件的差异

    1. vimdiff $ vimdiff in.txt out.txt 垂直打开:  vimdiff   point.c     point-a.c 水平打开:   vimdiff -o  point ...

  3. ubuntu mongodb backup/restore (备份和恢复)

    备份(导出) 1.导出单个collection-.json格式 mongoexport --host:127.0.0.1 --port:27017 --db test --collection tes ...

  4. Python 实现画一个小猪佩奇

    ===================================== 看到 佩奇的广告片刷红,为了迎接猪年,咱们也来用Python  画板实现一个效果吧 from turtle import* ...

  5. 《DSP using MATLAB》Problem 3.17

    用差分方程两边进行z变换,再变量带换得到频率响应函数(或转移函数,即LTI系统脉冲响应的DTFT). 代码: %% ------------------------------------------ ...

  6. Google Review中Zlib.Portable报错的一种排查解决方案

    前几天遇到一个恶心的问题,跟同事一块解决了,在这里记录下过程. 比较懒,直接转他的blog: http://www.cnblogs.com/caochenghua/p/6530053.html 报错信 ...

  7. test20181007 wzoi

    题意 分析 考场40分 错误的Manacher+dp. 用\(f(i)\)表示\(s_{1 \sim i}\)的最长偶数回文覆盖长度,在Manacher的同时用刷表法转移,每次还要对\(f(i-1)\ ...

  8. 使用Reaction cli 创建应用

    默认简单跑起来,我们可以直接使用docker,同时官方也为我们提供了cli 工具,可以快速的创建应用 安装cli npm install -g reaction-cli 初始化项目 reaction ...

  9. sql server CLR

    1. 配置sql server 启用CLR 在SQL Server2005/2008里面,CLR默认是关闭的.可以使用如下SQL语句开启CLR. sp_configure 'show advanced ...

  10. php 的交互命令行

    php 的交互命令行 使用过 python 都知道 python 可以使用交互命令. 如下图: 但是 执行 php 显示这个是什么鬼? 按回车和加分号都没用,这是什么原因? 其实是因为使用 php 交 ...