列表相关
print("====================列表生成式开始=======================")
# 生成1-99的之间所有奇数的列表
nums = range(1, 99, 2)
print(nums, type(nums))
print("====================列表生成式结束=======================")
print("====================列表推导式开始=======================")
# 1-99之间的所有奇数平方之后的列表
list = [ele ** 2 for ele in range(1, 99) if ele % 2 == 1]
print(list)
print("====================列表推导式结束=======================")
print("====================列表的添加操作开始=======================")
# 追加到末尾
list = [ele for ele in range(6)]
print(list)
list.append(6)
print(list)
# 在指定索引处新增
list.insert(2, 2)
print(list)
# extend在列表中扩展一个可迭代序列
list.extend([1, "121"])
print(list)
list = list * 2
print(list)
# +连接 extend 会把字符串拆开,插入到列表,+则只能连接另一个集合
list = list + ["haha"]
print(list)
print("====================列表的添加操作结束=======================")
print("====================列表的删除操作开始=======================")
# 删除指定元素
del list[-1]
print(list)
# 返回删除元素,参数默认删除尾部元素
re_ele = list.pop()
print(re_ele)
print(list)
# remove:移除指定元素
list.remove("121")
print(list)
list.remove(0)
print(list)
# 删除所有的2和3
new_list = [1, 2, 2, 3, 4, 2, 2, 5, 2]
print([ele for ele in new_list if ele not in [2, 3]])
####遍历删除引发的问题
for num in new_list:
if num == 2:
new_list.remove(2)
print(new_list.count(2)) # 移除以后为何还存在2呢?移除元素后,后面元素往前移,会漏掉遍历元素
print("====================列表的删除操作结束=======================")
print("====================修改集合开始=======================")
# 通过下标改变指定元素
up_list = [1, 2, 3, 4, 6, 6]
up_list[-2] = 5
print(up_list)
print("====================修改集合结束=======================")
print("====================查询集合开始=======================")
# 查找元素所在的位置,并移除
find_list = [ele for ele in range(1, 10) if ele % 2 == 0]
find_list.append("haha")
find_list.insert(1, "haha")
print(find_list)
# [2, 4, 6, 8, "haha"]
if "haha" in find_list:
find_list.remove("haha")
print(find_list)
target_index = find_list.index("haha")
print(target_index)
# 切片操作
print(find_list[1:-1])
# 反转列表
print(find_list[::-1])
print("====================查询集合结束=======================")
print("====================遍历列表开始=======================")
# 获取集合元素以及索引 方式1
it_list = ["a", "a", "b", "a"]
current_index = 0
for ele in it_list:
print(ele, it_list.index(ele, current_index)) # 此处有问题,会输出两次"a"的索引
current_index += 1
# 获取集合元素以及索引 方式2 推荐:
for index in range(len(it_list)):
print(index, it_list[index])
# 获取集合元素以及索引 方式3 枚举对象
for index, value in enumerate(it_list):
print(index, value)
# 获取集合元素以及索引 方式4 迭代器
# 判定是否是可迭代对象
# 迭代器中的元素,只会在用到时才加载,在用到之前可以不存在,用完后可以被销毁
import collections
print(isinstance(it_list, collections.Iterable))
it = iter(it_list)
for ele in it:
print(ele)
print("====================遍历列表结束=======================")
print("====================排序开始=======================")
# sorted 并不会改变原来的列表,而是生成一个新列表
# sort 会在改变原来的列表
list = sorted(it_list)
print(list)
print(it_list)
tuple_data = [("ysj", 18), ("lt", 19), ("zxj", 20)]
def getKey(ele):
return ele[1]
print(sorted(tuple_data, reverse=True))
tuple_data.sort(reverse=True)
print(tuple_data)
print(sorted(tuple_data, key=getKey, reverse=True))
print("====================排序结束=======================")
print("====================乱序和反转开始=======================")
# 乱序 reversed内建函数 不会改变原列表,但是方法会改变原列表
import random
values = [1, 2, 3, 4, 5]
random.shuffle(values)
print(values)
# 反转
ret = reversed(values)
print(ret)
print(values.reverse(), values)
print("====================乱序和反转结束=======================")