python3 列表属性
1、合并
>>> l1=[1,2,3,'e']
>>> l2=['f',34,'feel']
>>> l1+l2
[1, 2, 3, 'e', 'f', 34, 'feel']
2、重复
>>> l1
[1, 2, 3, 'e']
>>> l1*2
[1, 2, 3, 'e', 1, 2, 3, 'e']
3、增加 append extend
>>> l1=[1,2,3,'e']
>>> l1.append(4)
>>> l1
[1, 2, 3, 'e', 4]
>>> l2=['hello','world']
>>> l1.extend(l2)
>>> l1
[1, 2, 3, 'e', 4, 'hello', 'world']
插入 insert 插入位置 插入内容
>>> l2.insert(2,'a')
>>> l2
['hello', 'world', 'a']
>>> l2.insert(4,'a')
>>> l2
['hello', 'world', 'a', 'a']
4、计数 L.count
>>> l2
['hello', 'world', 'a', 'a']
>>> help(list.insert)
>>> l2.count('a')
2
>>> l2.count('l')
0
5、搜索位置 L.index
index(...)
L.index(value, [start, [stop]]) -> integer -- return first index of value.
>>> l2
['hello', 'world', 'a', 'a']
>>> l2.index('a')
2
6、排序 sort
>>> l2
['hello', 'world', 'a', 'a']
>>> l2.sort()
>>> l2
['a', 'a', 'hello', 'world'] 直接改变列表
>>> l2.sort(reverse=True) 倒序排序设置reverse为True
>>> l2
['world', 'hello', 'a', 'a']
反转 reverse 切片
>>> l2
['world', 'hello', 'a', 'a']
>>> l2.reverse()
>>> l2
['a', 'a', 'hello', 'world']
>>> l2[::-1]
['world', 'hello', 'a', 'a']
7、删除元素
>>> l2
['a', 'a', 'hello', 'world']
>>> del l2[0]
>>> l2
['a', 'hello', 'world']
>>> l2.remove('hello') 按值删除
>>> l2
['a', 'world']
>>> l1=['a','hello',1,4,34,'er']
>>> l1.pop() 默认删除最后一个元素,
'er'
>>> l1
['a', 'hello', 1, 4, 34]
>>> l1.pop(0) 按索引删除
'a'
>>> l1
['hello', 1, 4, 34]
8、使用切片修改元素
>>> l1=[1,2,3,4,5,6,7,8,9,10]
>>> l1[0:8:2]=['a'] 不连续元素
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: attempt to assign sequence of size 1 to extended slice of size 4
>>> l1[0:8:2]=['a','b','c','d']
>>> l1
['a', 2, 'b', 4, 'c', 6, 'd', 8, 9, 10]
>>> l1=[1,2,3,4,5,6,7,8,9,10]
>>> l1[1:3]=['a'] 连续元素
>>> l1
[1, 'a', 4, 5, 6, 7, 8, 9, 10]
9、列表推导
>>> L=[x**2 for x in range(1,10) if x%2!=0]
>>> L
[1, 9, 25, 49, 81]
10、列表生成 list
>>> a=list('hello')
>>> a
['h', 'e', 'l', 'l', 'o']
python3 列表属性的更多相关文章
- [CSS]列表属性(List)
CSS 列表属性(List) 属性 描述 CSS list-style 在一个声明中设置所有的列表属性. 1 list-style-image 将图象设置为列表项标记. 1 list-style- ...
- python3列表
Python3 列表 list python的矩阵 python中矩阵可以用双层列表表示 Python列表脚本操作符 len([1, 2, 3]) 3 长度 [1, 2, 3] + [4, 5, 6] ...
- Python直接改变实例化对象的列表属性的值 导致在flask中接口多次请求报错
错误原理实例如下: class One(): list = [1, 2, 3] @classmethod def get_copy_list(cls): # copy一份list,这样对list的改变 ...
- Python3 列表 copy() 方法
描述 Python3 列表 copy() 方法用于复制(浅拷贝)列表(父不变,子变),类似于 a[:]. 语法 copy() 方法语法: L.copy() 参数 无. 返回值 返回复制(浅拷贝)后的新 ...
- Python3 列表 clear() 方法
描述 Python3 列表 clear() 方法用于清空列表,类似于 del a[:]. 语法 clear() 方法语法: L.clear() 参数 无. 返回值 该方法没有返回值. 实例 以下实例展 ...
- python3 字符串属性(一)
python3 字符串属性 >>> a='hello world' >>> dir(a) ['__add__', '__class__', '__contains_ ...
- python009 Python3 列表
Python3 列表序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推.Python有6个序列的内置类型,但最常见的是 ...
- css列表属性和样式控制
如下图是360浏览器主页的内容,上边有导航,下边是新闻列表,这种布局很常见,今天就来学习css列表属性之后并制作它. 列表属性 html有三种类型的列表:无序列表,有序列表和自定义列表.设置列表标记有 ...
- css中的列表属性
list-style-type设定引导列表的符号类型,可以设置多种符号类型,值为disc.circle.square等 list-style-image使用图像作为定制列表的符号 list-style ...
随机推荐
- JavaWeb 之邮件发送
1. 邮件协议概述 SMTP(Simple Mail Transfer Protocol, 简单邮件传输协议) 发邮件协议; POP3(Post Office Protocol Version 3, ...
- rgba透明的兼容处理
background-color: rgba(0, 0, 0, .6);filter: progid:DXImageTransform.Microsoft.gradient(startColorstr ...
- Python 是怎么火起来的?
Python 之父 Guido 正在设计 Python 语言,结果家里突然潜入一条大蟒蛇,一番激烈斗争,大蟒蛇把 Guido 叔生吞进肚,并洋洋自得:So Who is Guido Van Rossu ...
- Linux中命令查找顺序
第一优先级:用绝对路径或相对路径执行的命令第二优先级:别名指定的命令第三优先级:Bash内部命令第四优先级:$PATH环境变量定义的目录查找顺序中找到的第一个命令
- Oracle11g用户频繁锁定并且解锁后不允许登录
原因有可能是oracle的密码过期机制导致的:一.由于Oracle中默认在default概要文件中设置了“PASSWORD_LIFE_TIME=180天”所导致.解决办法:1.查看用户用的哪种prof ...
- HTMLbutton控件中文字显示一直不居中
在写HTML时,发现HTML中button控件中文字显示一直不居中, 最后发现是在标签前出现了一个全角空格引起的. 在Emeditor中将不显示的字符(空格,全角空格,换行,制表符)设置为显示,就可以 ...
- PAT 天梯赛 L1-046. 整除光棍 【模拟除法】
题目链接 https://www.patest.cn/contests/gplt/L1-046 思路 用同余定理以及模拟除法. AC代码 #include <iostream> #incl ...
- Linux下运行java项目
最近初步接触了linux,感觉很有新鲜感.之前在windows下干过的事情也便想到在linux环境下实现一下.正好手头在编java,就想既然java可以在windows的DOS操作下运行,是不是也可以 ...
- OpenGL学习进程(2)OpenGL开发环境的搭建
通过本节,我们来学习一下在Win10 64.VS1013环境下搭建OpenGL的开发环境. (1)选择一个编译环境: 现在在windows中OpenGL的主流编译工具有Visual S ...
- String和StringBufffer的区别
string的字符串操作都是废弃已有的对象,开辟一个新的内存空间创建一个新的对象 比如一个string str= "字符串"; str += "a"; 这样的操 ...