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. spark 与 Hadoop 融合后启动 slf4j提示Class path contains multiple SLF4J bindings

    相关参考文献: https://www.oschina.net/question/93435_174549 警告信息如下: 看起来明明就是一个文件,怎么还提示multiple bindings呢,sl ...

  2. Word所有字体按比例缩小

    ctrl + [ 不然每次都要一部分一部分的修改啊

  3. OJ链接

    BNU..好难找..http://www.bnuoj.com

  4. HashMap Hashtable LinkedHashMap TreeMap

    // Map<String, String> map = new HashMap<String, String>(); // bb aa cc Map<String, S ...

  5. bulid-tool

    Build tool 中文构建工具.构建工具能够帮助你创建一个可重复的.可靠的.携带的且不需要手动干预的构建.构建工具是一个可编程的工具,它能够让你以可执行和有序的任务来表达自动化需求.假设你想要编译 ...

  6. 【liunx】端口号的占用情况查看

    Linux如何查看端口 1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000 # lsof -i:8000 COMMAND PID USER ...

  7. 【转】linux下解压.bz2压缩文件

    原文网址:http://zhidao.baidu.com/question/90378903.html tar-c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压 ...

  8. 【转】每天一个linux命令(8):cp 命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/10/29/2744185.html cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一 ...

  9. Angular 4 路由介绍

    Angular 4 路由 1. 创建工程 ng new router --routing 2. 创建home和product组件 ng g component home ng g component ...

  10. TroubleShoot: Enable Developer Mode in Windows 10 Insider Preview Build 10074

    There is a known issue in Windows 10 Insider Preview build 10074 (see here). Developers cannot enabl ...