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 ...
随机推荐
- C语言结构体数组内带字符数组初始化和赋值
1.首先定义结构体数组: typedef struct BleAndTspRmtCmd{ char terminal[3]; char note[3]; char rmtCmd[10]; char c ...
- exe4j中"this executable was created with an evaluation version of exe4j"
在使用exe4j时,如果您的exe4j没有注册,在运行有exe4j转换的*.jar为*.exe的可执行文件是会提示:"this executable was created with an ...
- Oracle 12c 读书笔记(二):Oracle数据库体系结构
以11g来分析 数据库实例包括:SGA和一系列后台管理.监控简称 数据库包括三种文件:数据文件.控制文件.重做日志文件 数据库实例和数据库是Orale数据库体系的核心部分 Oracle服务器和实例 实 ...
- python Flask框架CBV视图
1.演示之前需要先写一个装饰器 # 装饰器 def wrapper(func): def inner(*args, **kwargs): print('操作函数%s之前' % func.__name_ ...
- Django中_Meta 部分用法
周一了,就不长篇大论了,给大家分享一个很实用的知识点,希望大家周末过得开心,愉快,诗和远方在等着你们.而我还在苦逼的撸代码,只为了应付眼前的苟且! model.UserInfo._meta.app_l ...
- PyNest——part 2: populations of neurons
part 2: populations of neurons introduction 在这篇讲义中,我们着眼于创建和参数化神经元批次,并将它们连接起来. 当你完成这些材料时,你会知道如何: 创建具有 ...
- self-awareness is key in changing the way you think
1: controlling the way you think is a manageable process The good news is that you have control over ...
- 百度地图sn计算方法说明
官方说明[有状态码的数值意义查询 和 python 转换源码]: http://lbsyun.baidu.com/index.php?title=lbscloud/api/appendix
- Python2 socket 多线程并发 TCPServer Demo
#coding=utf-8 import socket import threading,getopt,sys,string opts, args = getopt.getopt(sys.argv[1 ...
- asp.net 下载图片
public class DownLoad : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Res ...