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. How to return NULL string

    Q: std::string get_file_contents(const char *filename) { std::ifstream in(filename, std::ios::in | s ...

  2. opencv-python教程学习系列8-opencv图像算术运算

    前言 opencv-python教程学习系列记录学习python-opencv过程的点滴,本文主要介绍图像的算术运算,坚持学习,共同进步. 系列教程参照OpenCV-Python中文教程: 系统环境 ...

  3. 将整个文件夹推上github

    /*游戏或者运动才能让我短暂的忘记心痛,现如今感觉学习比游戏和运动还重要——曾少锋*/ 首先你要有一个本地仓库和一个远程仓库(github中的仓库).git文件夹下面不能含有 git文件夹,如果有的话 ...

  4. BZOJ3879: SvT【后缀数组+单调栈】

    Description (我并不想告诉你题目名字是什么鬼) 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个询问,我们给出若干个后缀(以其在S中出现的起始 ...

  5. 51Nod 1009:1009 数字1的数量 (思维)

    1009 数字1的数量  基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的 ...

  6. 【C#】 增加多个分部类

    有时需要在一个类下面增加多个不同功能的分部类,或者是不同开发组员以其命名的分部类. eg: 首先创建一个类,改为分部类,partial.. 复制此类的文件,改一个文件名.然后修改项目文件.csproj ...

  7. Json.NET Updates: Merge, Dependency Injection, F# and JSONPath Support

    Json.NET 6.0 received 4 releases this year, the latest last week. Over these releases, several new f ...

  8. [连载]Java程序设计(03)---任务驱动方式:寻找高富帅和屌丝

    版权声明:本文为博主原创文章,请在转载时说明出处. https://blog.csdn.net/jackfrued/article/details/26163877 任务:相同在上一家公司.公司还须要 ...

  9. [转]Spring事务<tx:annotation-driven/>

    在使用SpringMVC的时候,配置文件中我们经常看到 annotation-driven 这样的注解,其含义就是支持注解,一般根据前缀 tx.mvc 等也能很直白的理解出来分别的作用.<tx: ...

  10. Tomcat 自动化部署

    Tomcat 自动化部署脚本 使用方法: ./autodeploy.sh test 其中autodeploy.sh 为脚本的文件名, test为war的文件名. #!/bin/sh now=`date ...