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. 在python中的使用

    操作步骤: 1. 连接数据库,生成数据库连接对象 conn = pymongo.MongoClient('localhost',27017) 2. 选择要操作的数据库,生成数据库对象 (__setit ...

  2. Python3中 sys.argv的用法

    sys.avgr 是一个Python的引用模块.刚好做一个作业需要用到它,在sublime上编辑后运行,试图从结果发现它的用途,然而结果一直都是没结果. 后面在网上查了资料,才明白过来.sys.arg ...

  3. Python的学习之-计算机编码和二进制

    bit位,计算机中最小的表示单位 8bit = 1bytes字节,最小的储存单位,1bytes缩写为1b 1KB = 1024B 1MB = 1024KB 1GB = 1024MB 1TB = 102 ...

  4. Anaconda+Tensorflow环境安装与配置

    转载请注明出处:http://www.cnblogs.com/willnote/p/6746499.html Anaconda安装 在清华大学 TUNA 镜像源选择对应的操作系统与所需的Python版 ...

  5. Hibernate有五大核心接口,分别是:Session Transaction Query SessionFactoryConfiguration

    Session接口: Session接口 Session 接口对于Hibernate 开发人员来说是一个最重要的接口.然而在Hibernate中,实例化的Session是一个轻量级的类,创建和销毁它都 ...

  6. MySQL账号安全设置

    ======================================================================== 推荐账号安全设置 在数据库服务器上严格控制操作系统的账 ...

  7. Mac OS X 10.9.2 配置 jdk1.8.0_05 环境变量

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/gcvdsvb/article/details/24543543 首先到 oracle 站点下载 ss ...

  8. TensorFlow学习线路

    如何高效的学习 TensorFlow 代码? 或者如何掌握TensorFlow,应用到任何领域? 作者:黄璞链接:https://www.zhihu.com/question/41667903/ans ...

  9. EasyPHP-Devserver-17的坑位

    mysql登陆错误:error: 'Plugin '*2A8AF30E682613A2F1CE1E28BA11D8560B294DCE' is not loaded' http://stackover ...

  10. ORTP库移植

    转载,侵删 1.ORTP的引入 为什么要使用RTP:http://blog.51cto.com/ticktick/462746RTP协议分析:http://www.xuebuyuan.com/7399 ...