Pyhton:List build-in function
列表是Python中的可迭代对象之一,在讲列表的内建函数之前我们可以自己在IDE上看看都有那些内建函数,我们可以在pycharm中使用代码及其运行结果如下:
print(dir(list)) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
前面的带有__的函数我们先不管,可以看一下后面的append等函数,我们一一举例子来看
1.append的中文意思是追加
aList = ['1','2','3']
aList.append('4') # append 默认添加到列表最好
print(aList) >>>['1','2','3','4']
2.clear的中文意思是清除,顾名思义
aList.clear() # 清空列表的内容
print(aList) >>>[]
3.copy 拷贝,此处为浅拷贝
aList = ['1','2','3']
bList = aList.copy()
print(bList)
>>>['1','2','3']
4.count 计数
aList = [1,2,3,2]
n = aList.count(2) # 返回一个对象在列表中出现的次数
print(n) >>>2
5.extend 扩展
aList = [1,2,3,2]
bList = [4,5,6]
aList.extend(bList) # extend 中的参数是一个序列,将这个序列添加到列表中
print(aList) >>>[1,2,3,2,4,5,6]
6.index 索引
aList = [1,2,3,2]
n = aList.index(2) # 返回索引到的第一个参数所在列表的位置值
print(n) >>> 1 u = aList.index(2,2,len(aList)) # 后面两个参数是索引范围,分别是star 和 end
print(u) >>> 3
7.insert 插入
aList = [1,2,3,2]
aList.insert(2,4) # 前面的参数表示插入的位置,后面的参数表示要插入的值
print(aList) >>> [1, 2, 4, 3, 2]
8.pop 删除
aList = [1,2,3,2]
aList.pop(1) # pop 不加参数默认删除最后一个
print(aList) >>> [1, 3, 2]
9.remove 删除
aList = [1,2,3,2]
aList.remove(1) # 参数为vaule
print(aList) >>>[2, 3, 2] aList.remove(aList[1])
print(aList) >>>[2,2]
10.reverse 反转
aList = [1,2,3,2]
aList.reverse() # 原地翻转列表
print(aList) >>>[2, 3, 2, 1]
11.sort 排序
aList = ['sad','successfully','hello']
aList.sort()
print(aList) >>>['hello', 'sad', 'successfully'] aList.sort(key = len,reverse=True) # sort 排序函数可以指定方式排列,此处为按照长度排列。如果reverse设置成Ture,责列表以反序排列
print(aList) >>>['successfully', 'hello', 'sad']
以上
Pyhton:List build-in function的更多相关文章
- Dreamweaver 扩展开发: Calling a C++ function from JavaScript
After you understand how C-level extensibility works in Dreamweaver and its dependency on certain da ...
- 编译PHP 报错:node.c: In function dom_canonicalization
编译PHP 报错:node.c: In function dom_canonicalization /opt/php-5.2.17/ext/dom/node.c:1953: error: deref ...
- VS报错:The build tools for v140 (Platform Toolset = 'v140') cannot be found
VS低版本打开高版本常会出现的错: The build tools for v140 (Platform Toolset = 'v140') cannot be found. To build usi ...
- 每日学习心得:$.extend()方法和(function($){...})(jQuery)详解
2014-02-09 前言: 节后头两天上班,主要是调整工作状态.项目也不是很紧,趁着周末把年前遇到了一些关于JS和JQuery的一些问题给总结一下.主要是介绍JQuery的extend方法和(fun ...
- VScript 函数调用的两种分类:Sub过程和Function过程
来源:http://soft.zdnet.com.cn/software_zone/2007/0925/523318.shtml 在 VBScript 中,过程被分为两类:Sub 过程和 Functi ...
- 使用CI遇到的问题报错:Call to undefined function base_url()
问题来源:在HTML文件中使用base_url()函数引入CSS文件时,发现报错:Call to undefined function base_url() 研究了一下才知道是因为没有加载url小助手 ...
- webpack报错:Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module 'babel-preset-env' from '...' - Did you mean "@babel/env"?
webpack报错:Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find mo ...
- Guava学习笔记之Maps(1):Maps.uniqueIndex(Iterable, Function)
Guava官方文档 https://github.com/google/guava/wiki/CollectionUtilitiesExplained 官方文档这样描述: [`Maps.uniqueI ...
- 杂项-Grunt:grunt build 打包和常见错误
ylbtech-杂项-Grunt:grunt build 打包和常见错误 1. 安装.打包返回顶部 1. npm WARN deprecated coffee-script@: CoffeeScrip ...
- Vim出现:_arguments:450: _vim_files: function definition file not found的问题解决
安装了zsh之后使用vim出现如下错误: arguments:450: _vim_files: function definition file not found _arguments:450: _ ...
随机推荐
- 洛谷 [P1939] 矩阵加速数列
矩阵快速幂模版 #include <iostream> #include <cstring> #include <cstdlib> #include <alg ...
- poj 3461 hash解法
字符串hash https://blog.csdn.net/pengwill97/article/details/80879387 https://blog.csdn.net/chaiwenjun00 ...
- mysql主从库
http://wangwei007.blog.51cto.com/68019/965575 一.mysql主从的原理 1.Replication 线程 Mysql的 Replication 是一个异步 ...
- 济南day1
预计分数:100+100+30 实际分数:10+60+20 T1立方数(cubic) 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数,例如1,8, ...
- 使用fastJson把对象转字符串首字母大小写问题的解决
例如:文档中要求传输的字段为 但是转成json字符串后却变成了: 解决方式: 在实体类的get方法上添加@JSONField(name = " ") 注解后问题解决: 输出:
- iOS 内存管理策略
内存管理策略(memory Management Policy) NSObject protocol中定义的的方法和标准命名惯例一起提供了一个引用计数环境,内存管理的基本模式处于这个环境中.NSObj ...
- Matlab多项式拟合測试
x=0:0.2:4; %生成等差数列 rnd=rand(1,size(x,2))*5; %生成一组随机数 y=x.*x.*x+x.*x+6+rnd; %生成y=x^3+x^2+6函数在垂直方向5个尺度 ...
- error LNK2019 无法解析的外部符号------类模板和内敛函数
今天用类模型实现一个单链表,开始是.h和.cpp将类模板的声明与实现分开写的,结果总是报错: 错误 error LNK2019: 无法解析的外部符号 ?$SingleList@H@@QAE@XZ),该 ...
- 转: memcached Java客户端spymemcached的一致性Hash算法
转自:http://colobu.com/2015/04/13/consistent-hash-algorithm-in-java-memcached-client/ memcached Java客户 ...
- ZT:与其怨天尤人,不如全力以赴;若想改变世界,你必须先从改变自己开始!
在闻名世界的威斯特敏斯特大教堂地下室的墓碑林中,有一块名扬世界的墓碑.其实这只是一块很普通的墓碑,粗糙的花岗石质地,造型也很一般,同周围那些质地上乘.做工优良的亨利三世到乔治二世等二十多位英国前国王墓 ...