码迷,mamicode.com
首页 > 编程语言 > 详细

Python3-笔记-B-001-数据结构-列表list[ ]

时间:2017-10-25 18:06:14      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:size   xtend   有序   类型   判断   ext   for   键值   元素   

# 列表[有序可变序列]
def lists():
# --- 列表 ---
# 列表可通过 append() / pop() 方法,作为栈使用
# 列表可通过 deque() 封装,作为双向队列使用
# 创建
lists = ["a", "b", "c"] # 列表
lists = list() # 空列表
lists = list((1, 2, 3)) # (将 元组 转为列表 (:将字典转为列表会损失数据)
lists = [i for i in range(10)] # 其他:[(10,i) for i in range(10)]

# 统计
length = len(lists) # 列表长度
count = lists.count("c") # 统计该元素数量

# 获取
str0 = lists[0] # 取出列表中的数据
lists = lists[0:3]
lists = lists[0:3:2]
ary2 = lists.copy() # 浅拷贝列表

# 查找
lists = ["a", "b", "c", ‘b‘, ‘a‘, ‘c‘, ‘d‘, ‘b‘, ‘a‘, ‘b‘] # 列表
index = lists.index("b") # 查询该元素的位置, 不存在抛异常(注意) => 1
index = lists.index("b", 2) # 起始位置,默认是0 =>3
index = lists.index("b", 4, 8) # 终止位置,默认是列表长度 =>7
elem = min(lists)
elem = max(lists)

# 遍历
for i in lists:
print("for:%s" % i)

# 修改
lists[0] = "0" # 修改第一个数据
lists.reverse() # 倒序
lists.sort() # (自然顺序)排序

# 添加
lists.append("d") # 添加数据(加到末尾)
lists.insert(1, "a1") # 插入数据
lists.extend(["d", "e", "f"]) # 添加数据(元素追加到末尾)
lists = [1, 2] + [3, 4] # 合并列表
lists = [1, 2] * 3 # 添加元素到自身3

# 删除
del lists[1] # 根据索引删除(注意)
lists.pop() # 删除最后元素
lists.remove(1) # 根据元素删除, 找不到则抛一行
lists.clear() # 清空列表

# 判断
boolean = "a" in lists # 该元素是否存在于列表中
boolean = "a" not in lists # 该元素是否不存在于列表中

# 排序
lists = sorted(lists) # 字典(无序可变的映射类型,键值对集合,且键唯一)

Python3-笔记-B-001-数据结构-列表list[ ]

标签:size   xtend   有序   类型   判断   ext   for   键值   元素   

原文地址:http://www.cnblogs.com/vito13/p/7729944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!