python基础

day1

1. hello world

"""
文档注释
"""

'''
多行注释
'''

print("hello world")
# 单行注释

2. 变量的使用

# 格式: 变量名 = 值

name = '牛圣三'
print(name)
# id() 查看内存变量
print(id(name))
# 注意: 变量首次赋值会被定义,再次赋值会改变变量的指向
name = '牛老板'
print(name)
print(id(name))
# 内存地址不同,说明是开辟了新空间

# 给多个变量赋值
a = b = 1
print(a, b)

# 给多个人变量赋不同的值
a, b = 5, 10
print(a, b)

# python 交换两个变量的值
a, b = b, a
print(a, b)

# 变量命名方法
# 1.数字,字母,下划线,数字不开头
# 2.符合大、小驼峰命名法,见名知意

3. 输入和输出

"""
输入和输出
"""
name = "牛圣三"
age = 24
# 输出:我叫 xx ,今年 xx 岁

# 方法一:不推荐
print("我叫", name, "今年", age, "岁")

# 方法二:不推荐
print("我叫%s,今年%d岁" % (name, age))

# 方法三:(推荐)需要填充变量的位置用{}代替,字符串尾部使用.formmat(name, age)
print("我叫{},今年{}岁".format(name, age))

# 换行输出
print("hello \nworld")

# 一行输出
print("hello", end=" ")
print("world")

'''
输出
'''

# (1)当程序执行到input时,等待用户输入,输入后才会继续运行
# (2)input 接收输入会保存在变量中
num = input("请输入一个整数:")
# type() 查看数据类型
print(type(num))
# <class 'str'>
num = int(num)
print(type(num))

# 例子
# 用户信息登记:提示用户输入姓名、年龄、性别、身高,并查看输入内容
name = input("请输入姓名:")
age = input("请输入年龄:")
sex = input("请输入性别:")
height = input("请输入身高:")

print("我叫{},今年{}岁,性别{},身高{}cm".format(name, age, sex, height))

4. 数据类型转换

# Python 为了应对不同的业务,把数据也分为不同的类型。
# 主要分为:
# 数值型:整型int,浮点型float,布尔型bool,复数型complex
# 字符串
# 列表
# 字典

# 元组
# 集合


num = 10
# 整形转换为浮点型
print(float(num)) # 10.0
# 浮点型转换为整形
num = 5.9
print(int(num)) # 5

# 其他类型转化为字符串,既在数据两边加上引号

# 其他类型转化为bool型:非空或非0转换为布尔就是True,否则是False
print(bool(10)) # True
print(bool(" ")) # True
print(bool("")) # False

print(int(True), float(True)) # 1 1.0

# eval()
print("3 + 5")
print(eval("3 + 5"))

5. 运算符

# 算数运算符 + - * / %  **(幂)  //(整除)
print(19 % 4) # 3
# 赋值运算符 =
# 复合赋值运算符 += -= *=
a = 1
a += 5 # 等价于 a = a + 5
print(a) # 6
# 逻辑运算符 and or not
# not 取反
print(not True) # False
print(not 5) # False
print(not 0) # True

# 何为真:非空或非0数据都是真
# 何为假:空或0都是假
# and 所有都是真才是真
a = True
b = False
print(a and b) # False
# 比较运算符 < > <+ >= != ==
print(5 > 3) # True
print(5 > 3 > 2) # True
print(10 > 5 < 8) # True, 等价于 10 > 5 and 5 < 8
# 成员运算符 in
 # 判断某个元素是否在容器中
string = "hello"
print("l" in string) # True
print("l" not in string) # False


# 练习
print("\n练习题目")
# 输入一个三位数,返回你逆序数
num = input("请输入一个三位数:")
print("正序输出为:" + num)

# 方法一:字符串的切片
print("逆序输出为:" + num[:: -1])  # 字符串的切片,直接返回逆序的数字

# 方法二:
num = int(num)
gws = num % 10
sws = num // 10 % 10
bws = num // 100
print("逆序数:{}".format(gws * 100 + sws * 10 + bws))

a = 1
b = 2
c = 3
d = 0
print(a and b and c) # 1 and 1 and 1 = 3
print(a and b and c and d) # 1 and 1 and 1 and 0 = 0
print(a and b or c and d) # (1 and 1) or (1 and 0) = 2
print(a and b or c and not d) # (1 and 1) or (1 and (not 0)) = 2

# not > and > or


6. 条件语句

# 单分支
# if 关键词后面的条件,必须是布尔类型的变量、可以转换为布尔类型的变量或者结果为
# 布尔类型的表达式
# if 条件后面,必须添加冒号,表示条件结束
# if 条件内部的代码,必须进行缩进,默认使用 4 个空格的缩进宽度

# 判断年龄是否满18岁

age = 20
if age >= 18:
    print("可以去网吧上网")

print("finish")

# 双分支
# if 关键词后面的 else 部分,是可选代码
# if 和 else 中的代码,在一个流程中不会同时执行
age = 15
if age >= 18:
    print("可以去网吧上网")
else:
    print("回家写作业")
print("finish")

# 多分支
# 多分支结构,可以包含多个 elif 关键字
# elif 是 else if 的缩写

# 判断分数的等价
score = float(input("请输入一个分数:"))
if score < 60:
    print("不及格")
elif 60 <= score < 80:
    print("良好")
elif 80 <= score < 90:
    print("优秀")
elif 90 <= score <= 100:
    print("大神")
else:
    print("输入有误")

# 分支嵌套
print("足球队练习:")
gender = "男"
age = 15

if gender == "男":
    if age >= 18:
        print("可以加入")
    else:
        print('不可以加入')
else:
    print('不符合条件')
# 猜拳游戏
# 人和电脑猜拳,判断输赢
# 人出拳: 0, 1, 2 代表 石头, 剪刀, 布
# 电脑猜拳:使用随机模块random 随机 0 1 2

import random
import time

player = input("请你出拳:(0/石头,1/剪刀,2/布)")


def playerVScom(playerInput):
    player = int(playerInput)
    # 电脑出拳
    com = random.randint(0, 2)
    print("电脑出拳", com)
    print("正在判断输赢...")
    time.sleep(1)
    # 判断输赢
    # 玩家赢
    if player == 0 and com == 1 or player == 1 and com == 2 or player == 2 and com == 0:
        print("恭喜你赢了")
    elif player == com:
        print("平局!")
    else:
        print("你弱爆了")


if len(player) > 1:
    print("出拳有误!")
    playerVScom(player)
else:
    playerVScom(player)

# 闰年 --> 被4整除但是不能被100整除的年份是普通闰年,被 400 整除的年份是世纪闰年
year = int(input("请输入年份:"))
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
    print("{}年是闰年".format(year))
else:
    print("{}年不是闰年".format(year))

# 3.个税计算器,输入税前工资,输出到手工资(选做).
# 扣除五险一金后,工资超过5000需要纳税
# 扣除五险一金后剩5500,超了500,需纳税500*0.03=15
# 扣除五险一金后剩7000,超了2000,盖纳税1500*0.03+500*0.1=95...简洁 算法: 2000*0.1-105=99
total_money = float(input("请输入税前工资:"))

# 五险一金比例:10.4 + 12 = 22.4
# 扣除五险一金后
wxyj_momey = total_money * 0.224
should_money = total_money * (1 - 0.224) - 5000
nashui = 0

if should_money > 0:
    if should_money <= 1500:
        nashui = should_money * 0.03
    elif 1500 < should_money <= 4500:
        nashui = should_money * 0.1 - 105
    elif 4500 < should_money <= 9000:
        nashui = should_money * 0.2 - 555
    elif 9000 < should_money <= 35000:
        nashui = should_money * 0.25 - 1005
    elif 35000 < should_money <= 55000:
        nashui = should_money * 0.3 - 2755
    elif 55000 < should_money <= 80000:
        nashui = should_money * 0.35 - 5505
    else:
        nashui = should_money * 0.45 - 13505

result = total_money - wxyj_momey - nashui
print("税前工资:{}元,五险一金:{}元,扣税:{}元,到手工资:{}元".format(total_money, wxyj_momey, nashui, result))



7. 循环结构

#
i = 0
while i <= 10000:
    print("你错了-{}".format(i))
    i += 1


for i in range(10000):
    print("for循环")

# for 循环,将迭代对象中的元素依次取出,赋值给临时变量
    for i in "hello":
        print(i)


"""
range(start, stop[, step])
start: 初始值
stop: 结束值(取不到)
step: 步长,可以为负,不写默认为 1
"""
# list 转换为列表
# list 转换为列表
# list 转换为列表
print(list(range(1, 5))) # 1 2 3 4
print(list(range(5))) # 0 1 2 3 4
print(list(range(1, 5, 2))) # 1 3
print(list(range(5, 1, -2))) # 5 3


# 循环控制关键字
# break:跳出循环
# continue:跳出本次循环

# 输出1-10到5结束循环
for i in range(1, 11):
    print(i)
    if i == 5:
        break

print("==========================")

# 输出1-10到5跳过本次
for i in range(1, 11):
    if i == 5:
        continue
    print(i)


# 判断一个数是不是素数
flag = True
num = int(input("请输入一个整数:"))
for i in range(2, num):
    if num % i == 0:
        print("不是质数")
        flag = False
        break

if flag:
    print("是质数")


# for...else...语法
# 如果循环中有break,不走else,循环正常结束走else
for i in range(5):
    pass
    # if i == 3:
    #     break
else:
    print("else中的代码")


# for ... else ... 判断是不是质数
num = int(input("请输入一个整数:"))
for i in range(2, int(num/2)):
    if num % i == 0:
        print("不是质数")
        break
else:
    print("是质数")

8. 列表

# 定义列表

# 获取单个元素
lst = ["常婉约", "六抱起", "空压机", "文臣 "]
# 根据下标获取格式:列表[索引名]
# 获取 六抱起
print(lst[1])
# 获取 空压机
print(lst[-2])


# 切片获取多个元素
"""
列表[start:end:step]
start: 不写默认为0
end: 不写默认到尾部
step: 不写默认为1,可以是负
区间左开右闭
"""
# 取出 "常婉约", "六抱起"
print(lst[0:2:1])
print(lst[:2:])
# 取出 "六抱起", "空压机"
print(lst[1:3])
print(lst[1:-1])


9. 列表的增删改查

# 增 append() 可以将元素添加到列表尾部
lst = ["常婉约", "六抱起", "空压机", "文臣"]
# 将 原华东 添加到列表的尾部
lst.append("原华东")
print(lst)

# 删除
# pop() 根据索引删除 ,不写默认删除最后一个,会将删除的元素返回
rel = lst.pop(0)
print(rel)
print(lst)
# remove() 从左到右删除第一个符合条件的元素
lst = ["常婉约", "六抱起", "空压机", "文臣", "常婉约"]
lst.remove("常婉约")
print(lst)

# 改
lst = ["常婉约", "六抱起", "空压机", "文臣"]
# 根据索引修改元素 格式: 列表[索引] = 值
lst[1] = "六老板"
print(lst)

# 列表逆序
lst.reverse()
print(lst)

# 排序
lst = [2, 8, 3, 6, 7]
lst.sort() # 默认从小到大排序
lst.sort(reverse=True) # 从大到小排序
print(lst)

# 查看
# index() 返回从左道右第一个符合条件的元素的索引
lst = ["常婉约", "六抱起", "空压机", "文臣", "常婉约"]
print(lst.index("常婉约"))
#count() 统计元素出现的次数
print(lst.count("常婉约"))
# len() 计算容器的长度
print(len(lst))

lst = [2, 3, [5, 6, 7]]
print(len(lst))

# 列表循环嵌套
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# 取出 5
print(lst[1][1])

# 遍历列表
# 第一种:直接取出元素
lst = ["常婉约", "六抱起", "空压机", "文臣", "常婉约"]
for i in lst:
    print(i)

print("===================")

# 第二种:取出下表,根据下标取出元素
for i in range(len(lst)):
    print(lst[i])

10. 作业

# 标准作业
# 1. 输入一个数,判断是否是质数?

# print("第一题:")
# num = int(input("请输入一个整数:"))
# for i in range(2, int(num / 2) + 1):
#     if num % i == 0:
#         print("{}不是质数".format(num))
#         break
# else:
#     print("{}是质数".format(num))

# 2. 输出100-200范围内的所有质数

# print("第二题:")
# lst = []
# for num in range(100, 200):
#     for i in range(2, int(num / 2) + 1):
#         if num % i == 0:
#             break
#     else:
#         lst.append(num)
# print(lst)

# 3. 求0-100范围内能被7整除但是不带7的整数,并存储到列表中

# print("第三题:")
# lst = []
# for i in range(0, 100):
#     if i % 7 == 0 and (i // 10 != 7 and i % 10 != 7):
#         lst.append(i)
# print(lst)

# 4. 存在列表 a = [11, 22, 33], 如何向列表尾部添加新元素44,如何让向列表头部添加33

# print("第四题:")
# a = [11, 22, 33]
# a.append(44)
# a.insert(0, 33)
# print(a)

# 提升作业
# 1. 比较两个列表中的元素,找到不相同的元素并保存在列表3中

# print("提升第一题:")
# lst1 = ["Sun", "Mon", "Tue", "Wed", "Fri", "Sat"]
# lst2 = ["Sun", "Tue", "Thu", "Sat", "Tom"]
# lst3 = []
# for i in lst1:
#     if i not in lst2:
#         lst3.append(i)
# for i in lst2:
#     if i not in lst1:
#         lst3.append(i)
# print(lst3)

# 2. 写一段python代码实现删除一个list里面重复元素
# 千万不要在便利列表的同时删除列表里面的元素!!!

# print("提升第二题:")
# lst = [1, 2, 1, 3, 1, 4, 1, 5, 1]
# # 方法一
# IndexCount = lst.count(1)
# for i in range(IndexCount):
#     lst.remove(1)
# print(lst)

# # 方法二
# new_lst = []
# for i in lst:
#     if i != 1:
#         new_lst.append(i)
# print(new_lst)


# 3. 统计出列表中2的个数以及对应索引的值
# lst = [1, 2, 3, 4, 5, 6, 2]
# indexCount = []
#
# count_2 = lst.count(2)

# # 方法一:
# for i in range(len(lst)):
#     if lst[i] == 2:
#         indexCount.append(i)
#

# 方法二:
# for i in range(count_2):
#     index_2 = lst.index(2)
#     indexCount.append(index_2)
#     lst[index_2] = False
# print(lst)


# print("2的个数是{},对应的索引是{}".format(count_2, indexCount))


# 4. 数字炸弹
# import random
# bomb = random.randint(1, 101)
# start = 1
# end = 100
# for i in range(5):
#     player = int(input("请输入{}到{}之间的整数:".format(start, end)))
#     if player > 100 or player < 1:
#         print("输入有误!")
#         break
#     if player > bomb:
#         end = player
#         print("猜大了")
#     elif player < bomb:
#         start = player
#         print("猜小了")
#     else:
#         print("恭喜你猜对了")
#         break
# else:
#     print("炸弹数是:{}".format(bomb))

# 练习:使用列表嵌套,将8名同学随机分配到三个教室
rooms = [[], [], []]
students = ["常婉月", "刘宝琦", "孔维亚", "霺尘", "高雪", "马玉营", "刘金龙", "王胜利"]

"""
例如:
"孔维亚","霺尘","高雪"
"刘金龙","王胜利"
"常婉月","刘宝琦","马玉营"

输出:第1个教室有:"孔维亚","霺尘","高雪"
     第2个教室有:"刘金龙","王胜利"
     第3个教室有:"常婉月","刘宝琦","马玉营"
"""
import random

for i in students:
    index = random.randint(0, 2)
    rooms[index].append(i)
print("第1个教室有:{},\n第2个教室有:{},\n第3个教室有:{}".format(rooms[0], rooms[1], rooms[2]))

day2

1. 元组

# 声明一个元组(元组本身不可修改)
# 只有查询的方法

# 定义方法一
tup1 = tuple((1, 2, 3, 4, 5))  # (1, 2, 3, 4, 5) <class 'tuple'>
print(tup1, type(tup1))

# 定义方法二(推荐)
tup2 = (1, 2, 3, 4, 5)
print(tup2, type(tup2))  # (1, 2, 3, 4, 5) <class 'tuple'>

# 列表中有一个对象
lst = [1]
print(lst, type(lst))  # [1] <class 'list'>
tup = (1)
print(tup, type(tup))  # 1 <class 'int'>

# 元组中如果只有一个数据,需要写逗号
tup3 = (1,)
print(tup3, type(tup3))  # (1,) <class 'tuple'>

# T.index(dat) 获取元组中某个数据 dat 出现的位置
# T.count(dat) 获取元组中某个数据 dat 出现的次数
tup4 = ('a', 'b', 'c', 'd', 'e')
print(tup4.index("c")) # 2
print(tup4.count("c")) # 1

#当元组中有可变数据类型时,可修改可变数据类型
lst1 = ['a', 'b']
tup = (10, 20, lst1)
print(tup)
lst1.append('c')
print(tup)

2. 字典

"""
字典很重要,在爬虫中会经常用到
"""

# 声明字典(可修改)
# key 唯一不重复,value任意
# 与json类似,适合于给前端传数据
dic = {"name": "许新新", "age": 20, "height": 180}
print(dic)
# # help(dic.get) ,查找用法

# 增 格式:字典[key]=value
# 增加地址信息
dic["address"] = "济南"
print(dic)

# 随堂练习:某网站统计和记录当天的用户注册人数,保存形式为“日期:人数”,请将今天
# 的用户注册人数记录下来,日期和数据可以自定义
info = {"2021-01-01": 100, "2021-01-02": 300}
from datetime import datetime

date = str(datetime.now())[:10]
info[date] = 222
print(info)

# 删
# D.pop(key) 删除字典中 key 对应的 key:value 键值对数据
# D.popitem() 任意删除字典中的一对 key:value 键值对数据
# D.clear() 清空字典中的数据


dic = {"name": "许新新", "age": 20, "height": 180}
# # 删除年龄信息
ret = dic.pop("age")  # 会将删除的value值返回
print(ret)

dic.popitem() # 高版本删除最后一个键值对
print(dic)

dic.clear() # 清空字典,字典本身还保留
print(dic)


# 随堂练习:该网站只保存一年的数据,现需要将 2020 年的数据删除
info = {"2020-12-31": 10, "2021-01-01": 100, "2021-01-02": 300}
info.pop("2020-12-31")
# 改 格式:字典[key] = value
dic = {"name": "许新新", "age": 20, "height": 180}
dic["age"] = 21
print(dic)


# 查
dic = {"name": "许新新", "age": 20, "height": 180}

# D[key] 字典中根据 key 值获取对应的一个 key:value 键值对数据
print(dic["name"])
print(dic["name1"]) # 没有会报错(不推荐)


# D.get(key) 字典中根据 key 值获取对应的一个 key:value 键值对数据
print(dic.get("name"))
print(dic.get("name1"))
print(dic.get("name1", "路人甲"))  # 设置默认值,找不到就返回默认值
print(dic.get("name", "路人甲"))

# D.keys() 获取一个字典中的所有 key 组成的类集合数据
print(dic.keys())

# D.values() 获取一个字典中的所有 value 组成的类列表数据
print(dic.values())

# D.items() 获取一个字典中所有的 key:value 键值对数据
print(dic.items())

# 遍历key
for k in dic.keys():
    print(k)

for k in dic: # 直接遍历字典也是遍历key
    print(k)


# # 遍历values
for v in dic.values():
    print(v)

# 遍历key,values
for k, v in dic.items():
    print(k, v)


# 随堂练习:该网站只保存一年的数据,现需要将 2021 年的数据删除
info = {"2020-12-31": 10, "2021-01-01": 100, "2021-01-02": 300}

del_list = []
for k in info.keys():
    if "2021" in k:  # 要注重 in 关键字的使用
        del_list.append(k)
for k in del_list:
    info.pop(k)
print(info)


# 随堂练习:该网站现在年底需要汇总统计数据,请分别查看这一年中同统计了哪些日期?用
# 户量有多少,以及每一天的用户量分别是多少
info = {"2020-12-31": 10, "2021-01-01": 100, "2021-01-02": 300}
print(list(info.keys()))
print(sum(list(info.values())))


# 练习2
info = {"2020-12-31": 10, "2020-11-31": 10, "2021-01-01": 100, "2021-01-02": 300, "2022-5-31": 11}
# 统计一年的用户量
# {"2020":20,"2021",400,"2022":11}

# 法一:
value_2020 = 0
value_2021 = 0
value_2022 = 0
for k, v in info.items():
    if "2020" in k:
        value_2020 += v
    elif "2021" in k:
        value_2021 += v
    else:
        value_2022 += v

print(value_2020, value_2021, value_2022)

# 法二
new_info = {}
for k, v in info.items():
    if k[:4] not in new_info:
        new_info[k[:4]] = v
    else:
        new_info[k[:4]] += v
print(new_info)


#
a = {
    "001": {"name": "许新新", "age": 20, "address": "济南"},
    "002": {"name": "孔锦标", "age": 22, "address": "潍坊"},
    "003": {"name": "尹延嵘", "age": 18, "address": "德州"},
    "004": {"name": "曹茹", "age": 21, "address": "济南"},
    "005": {"name": "徐兴斌", "age": 25, "address": "曹县"}
}
# 1.取出003对应的信息
print(a.get("003"))
# 2.给所有的济南人加10000块住房补贴
# 法一:
for k, v in a.items():
    if v.get("address") == "济南":
        v["money"] = 10000
for k, v in a.items():
    print([k, v])

# 法二:
for v in a.values():
    if v.get("address") == "济南":
        v["money"] = 10000
for k, v in a.items():
    print([k, v])

# 3.输出年龄最小的信息
# 法一:
mix_age = 100
index = ""
for k, v in a.items():
    age = v.get("age")
    if age < mix_age:
        mix_age = age
        index = k
print(a.get(index))

# 法二
age_list = []
for dic in a.values():
    age_list.append(dic.get("age"))
min_age = min(age_list)  # 取得最小值
for dic in a.values():
    if dic.get("age") == min_age:
        print(dic)

3. 字符串

"""
字符串和元组一样,是不可变数据类型
"""
string = "hello world"
# find,查找某个字符在字符串中的位置
print(string.find("w"))  # 6
print(string.find("l"))  # 2
print(string.rfind("l"))  # 9 最右边的l

# count() 返回某个字符出现的次数
print(string.count("l"))


string = "hello world"
# S.replace(o, n, c) 的 将目标字符串中的 o 替换成 n, , 默认全部替换 ,换 可以指定替换 c 个 个
# 将所有的l换成x
ret = string.replace("l", "x")
print(ret)

# S.split(c) 串 使用字符串 c 对目标字符串 S 进行拆分,返回拆分后的列表
string2 = "a*b*c*d"
# 按照 * 对字符串进行分割,返回列表
ret2 = string2.split("*")
print(ret2)

# S.join([dats]) 串 使用字符串 S 对序列数据 dats 进行拼接,返回拼接后的字符串
lst = ["2022", "05", "11"]
ret3 = "-".join(lst)
print(ret3)

# S.lower() 串 将字符串 S 全部转换成小写
# S.upper() 串 将字符串 S 全部转换成大写
# S.strip() 剔除字符串两侧的无效空格

# 练习1:备份文件名:如输入文件名,加上“_副本”输出
# demo1.py-->demo1_副本.py
# hello.text-->hello_副本.text

string4 = "demo1.py"
# 法一:
ret4 = string4.split(".")
ret5 = "_副本.".join(ret4)
print(ret5)

# 法二:
string5 = "hello.text"
ret5 = string5.replace(".", "_副本.")
print(ret5)

#
file_name = "1.2.3.txt"
my_index = file_name.rfind(".")
print(file_name[:my_index] + "_副本" + file_name[my_index:])


# 练习2:输出一段话,逆序输出
# 如: hell xiao mi --->  mi xiao hello
content = "hello xiao mi"
lst = content.split()[::-1]
new_content = " ".join(lst)
print(new_content)


4. 列表推导式

# 格式: 变量=[表达式 for 临时变量 in 序列数据]
# 将序列中的数据依次取出赋值给临时变量,作用到表达式上,将结果添加到列表中
# 格式: 变量=[表达式 for 临时变量 in 序列数据 if 条件]
# 将序列中的数据依次取出赋值给临时变量,然后进行条件判断,符合要求的再作用到表达式上,将结果添加到列表中


lst = [1, 2, 3, 4, 5]
# 给所有的数据 +10
new_lst = [i + 10 for i in lst]
print(new_lst)

# 给所有的偶数 +10
lst = [1, 2, 3, 4, 5]
new_lst = [i + 10 for i in lst if i % 2 == 0]
print(new_lst)

# 变量=[表达式 for 临时变量1 for 临时变量2 in 序列数据]
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_lst = [j for i in lst for j in i]
print(new_lst)

# 练习 1:将 lst 中每一个元素进行平方后放入到一个新列表中,lst = [1, 2, 3, 4, 5]
lst = [1, 2, 3, 4, 5]
new_list = [i ** 2 for i in lst]  # **2 ---> 表示平方
print(new_list)

# 练习 2:将 lst 中的奇数放到一个新列表中,lst = [1, 2, 3, 4, 5, 6, 7, 8]
lst = [1, 2, 3, 4, 5, 6, 7, 8]
new_list = [i for i in lst if i % 2 != 0]
print(new_list)


5. 字典推导式

# 变量 = {k 表达式:v表达式 for 变量 in 序列数据}
# 变量 = {k 表达式:v表达式 for 变量 in 序列数据 if 条件}

# 交换字典中的 key 和 value 数据
dic = {"name": "天微微", "age": 18}
# 法一:
new_dic = {}
for k, v in dic.items():
    # 源字典的v做新字典的k,源自点的k做新字典的v
    new_dic[v] = k
print(new_dic)

# 法二:(字典推导式)
new_dic = {v: k for k, v in dic.items()}
print(new_dic)


6. 练习

# 练习 1:过滤掉长度小于 3 的字符串列表,并将剩下的转换成大写字母
string = ["aaaaa", "bb", "ccc", "d"]
new_string = [i.upper() for i in string if len(i) >= 3]
print(new_string)

# 练习 2:求(x,y)其中 x 是 0-5 之间的偶数,y 是 0-5 之间的奇数组成的元祖列表
lst = [(0, 1), (2, 3), (3, 5)]
new_lst = [(x, y) for x in range(0, 6, 2) for y in range(1, 6, 2)]

# 练习 3:例 3 求 M 中 3,6,9 组成的列表 M = [[1,2,3],[4,5,6],[7,8,9]]
M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_M = [i[2] for i in M]
print(new_M)

# 练习 4:求 M 中 1,5,9 组成的列表 M = [[1,2,3],[4,5,6],[7,8,9]]
# 分析 1,5,9 分别在第0个列表的第0个,第1个的第1个,第2个的第2个
M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_M2 = [M[i][i] for i in range(len(M))]
print(new_M2)

# 练习 5:需求:用户输入一些符号,请统计数字个数,字母个数,以及其他符号数量
# player = input("请输入一串带有数字和字母的符号:")
player = "123as45df67gh89jk"
str_sum = 0
dig_sum = 0
for i in player:
    if i.isdigit():  # 判断是不是数字
        dig_sum += 1
    elif i.isalpha():  # 判断是不是字母
        str_sum += 1
print("数字的个数是:{},字母的个数是:{}。".format(dig_sum, str_sum))

# 练习 6:判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)都是一样的整数
num = 1233321
new_num = int(str(num)[::-1])
if num == new_num:
    print("{} 是回文".format(num))
else:
    print("{} 不是回文".format(num))

# 练习 7:存在列表 a = [6,5,4,19,17,11,13,43,55,66,77,14,87,26,22,34,39,88,76,21,96,33,51,52],列表中元素不重复,如何修改列表中的元素 22 为 55
a = [6, 5, 4, 19, 17, 11, 13, 43, 55, 66, 77, 14, 87, 26, 22, 34, 39, 88, 76, 21, 96, 33, 51, 52]
index = a.index(22)
a[index] = 55
print(a)
# 练习 8:给定列表 a 和整数 n,返回 a 中出现次数大于 n 的所有元素。
# 样例: a=[1,2,3,1,3,4],n=1, 返回[1, 3]。
a = [1, 2, 3, 1, 3, 4]
n = 1

# 法一:
# set 是一个集合,起到去重的作用
print(list(set([i for i in a if a.count(i) > n])))

# 法二:
new_a = []
for i in a:
    if a.count(i) > n and i not in new_a:
        new_a.append(i)
print(new_a)

7. 作业

hero.json

[{

	"ename": 105,

	"cname": "廉颇",

	"title": "正义爆轰",

	"new_type": 0,

	"hero_type": 3,

	"skin_name": "正义爆轰|地狱岩魂"

}, {

	"ename": 106,

	"cname": "小乔",

	"title": "恋之微风",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "恋之微风|万圣前夜|天鹅之梦|纯白花嫁|缤纷独角兽"

}, {

	"ename": 107,

	"cname": "赵云",

	"title": "苍天翔龙",

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 4,

	"skin_name": "苍天翔龙|忍●炎影|未来纪元|皇家上将|嘻哈天王|白执事|引擎之心"

}, {

	"ename": 108,

	"cname": "墨子",

    "title": "和平守望",   

	"new_type": 0,

	"hero_type": 2,

	"hero_type2": 1,

	"skin_name": "和平守望|金属风暴|龙骑士|进击墨子号"

}, {

	"ename": 109,

	"cname": "妲己",

	"title": "魅力之狐",

	"pay_type": 11,

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "魅惑之狐|女仆咖啡|魅力维加斯|仙境爱丽丝|少女阿狸|热情桑巴"

}, {

	"ename": 110,

	"cname": "嬴政",

	"title": "王者独尊",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "王者独尊|摇滚巨星|暗夜贵公子|优雅恋人|白昼王子"

}, {

	"ename": 111,

	"cname": "孙尚香",

	"title": "千金重弩",

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "千金重弩|火炮千金|水果甜心|蔷薇恋人|杀手不太冷|末日机甲|沉稳之力"

}, {

	"ename": 112,

	"cname": "鲁班七号",

	"title": "机关造物",

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "机关造物|木偶奇遇记|福禄兄弟|电玩小子|星空梦想"

}, {

	"ename": 113,

	"cname": "庄周",

	"title": "逍遥梦幻",

	"new_type": 0,

	"hero_type": 6,

	"hero_type2": 3,

	"skin_name": "逍遥幻梦|鲤鱼之梦|蜃楼王|云端筑梦师"

}, {

	"ename": 114,

	"cname": "刘禅",

	"title": "暴走机关",

	"new_type": 0,

	"hero_type": 6,

	"hero_type2": 3,

	"skin_name": "暴走机关|英喵野望|绅士熊喵|天才门将"

}, {

	"ename": 115,

	"cname": "高渐离",

	"title": "叛逆吟游",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "叛逆吟游|金属狂潮|死亡摇滚"

}, {

	"ename": 116,

	"cname": "阿轲",

	"title": "信念之刃",

	"new_type": 0,

	"hero_type": 4,

	"skin_name": "信念之刃|爱心护理|暗夜猫娘|致命风华|节奏热浪"

}, {

	"ename": 117,

	"cname": "钟无艳",

	"title": "野蛮之锤",

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 3,

	"skin_name": "野蛮之锤|生化警戒|王者之锤|海滩丽影"

}, {

	"ename": 118,

	"cname": "孙膑",

	"title": "逆流之时",

	"new_type": 0,

	"hero_type": 6,

	"hero_type2": 2,

	"skin_name": "逆流之时|未来旅行|天使之翼|妖精王"

}, {

	"ename": 119,

	"cname": "扁鹊",

	"title": "善恶怪医",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "善恶怪医|救世之瞳|化身博士|炼金王"

}, {

	"ename": 120,

	"cname": "白起",

	"title": "最终兵器",

	"new_type": 0,

	"hero_type": 3,

	"skin_name": "最终兵器|白色死神|狰|星夜王子"

}, {

	"ename": 121,

	"cname": "芈月",

	"title": "永恒之月",

	"new_type": 0,

	"hero_type": 2,

	"hero_type2": 3,

	"skin_name": "永恒之月|红桃皇后|大秦宣太后|重明"

}, {

	"ename": 123,

	"cname": "吕布",

	"title": "无双之魔",

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 3,

	"skin_name": "无双之魔|圣诞狂欢|天魔缭乱|末日机甲|猎兽之王"

}, {

	"ename": 124,

	"cname": "周瑜",

	"title": "铁血都督",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "铁血都督|海军大将|真爱至上"

}, {

	"ename": 126,

	"cname": "夏侯惇",

	"title": "不羁之风",	

	"pay_type": 10,

	"new_type": 0,

	"hero_type": 3,

	"hero_type2": 1,

	"skin_name": "不羁之风|战争骑士|乘风破浪|无限飓风号"

}, {

	"ename": 127,

	"cname": "甄姬",

	"title": "洛神降临",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "洛神降临|冰雪圆舞曲|花好人间|游园惊梦"

}, {

	"ename": 128,

	"cname": "曹操",

    "title": "鲜血枭雄",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "鲜血枭雄|超能战警|幽灵船长|死神来了|烛龙"

}, {

	"ename": 129,

	"cname": "典韦",

	"title": "狂战士",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "狂战士|黄金武士|穷奇"

}, {

	"ename": 130,

	"cname": "宫本武藏",

	"title": "剑圣",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "剑圣|鬼剑武藏|未来纪元|万象初新|地狱之眼|霸王丸"

}, {

	"ename": 131,

	"cname": "李白",

	"title": "青莲剑仙",

	"new_type": 0,

	"hero_type": 4,

	"skin_name": "青莲剑仙|范海辛|千年之狐|凤求凰|敏锐之力"

}, {

	"ename": 132,

	"cname": "马可波罗",

    "title": "远游之枪",

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "远游之枪|激情绿茵|逐梦之星"

}, {

	"ename": 133,

	"cname": "狄仁杰",

	"title": "断案大师",

	"pay_type": 11,

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "断案大师|锦衣卫|魔术师|超时空战士|阴阳师"

}, {

	"ename": 134,

	"cname": "达摩",

	"title": "拳僧",

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 3,

	"skin_name": "拳僧|大发明家|拳王"

}, {

	"ename": 135,

	"cname": "项羽",

	"title": "霸王",

	"new_type": 0,

	"hero_type": 3,

	"skin_name": "霸王|帝国元帅|苍穹之光|海滩派对|职棒王牌|霸王别姬|科学大爆炸"

}, {

	"ename": 136,

	"cname": "武则天",

	"title": "女帝",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "女帝|东方不败|海洋之心"

}, {

	"ename": 139,

	"cname": "老夫子",

	"title": "万古长明",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "万古长明|潮流仙人|圣诞老人|功夫老勺"

}, {

	"ename": 140,

	"cname": "关羽",

	"title": "一骑当千",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "一骑当千|天启骑士|冰锋战神|龙腾万里"

}, {

	"ename": 141,

	"cname": "貂蝉",

	"title": "绝世舞姬",

	"new_type": 0,

	"hero_type": 2,

	"hero_type2": 4,

	"skin_name": "绝世舞姬|异域舞娘|圣诞恋歌|逐梦之音|仲夏夜之梦"

}, {

	"ename": 142,

	"cname": "安琪拉",

	"title": "暗夜萝莉",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "暗夜萝莉|玩偶对对碰|魔法小厨娘|心灵骇客|如懿"

}, {

	"ename": 144,

	"cname": "程咬金",

	"title": "热烈之斧",

	"new_type": 0,

	"hero_type": 3,

	"hero_type2": 1,

	"skin_name": "热烈之斧|爱与正义|星际陆战队|华尔街大亨|功夫厨神"

}, {

	"ename": 146,

	"cname": "露娜",

	"title": "月光之女",

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 2,

	"skin_name": "月光之女|哥特玫瑰|绯红之刃|紫霞仙子|一生所爱"

}, {

	"ename": 148,

	"cname": "姜子牙",

	"title": "太古魔导",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "太古魔导|时尚教父"

}, {

	"ename": 149,

	"cname": "刘邦",

	"title": "双面君主",

	"new_type": 0,

	"hero_type": 3,

	"skin_name": "双面君主|圣殿之光|德古拉伯爵"

}, {

	"ename": 150,

	"cname": "韩信",

	"title": "国士无双",

	"new_type": 0,

	"hero_type": 4,

	"skin_name": "国士无双|街头霸王|教廷特使|白龙吟|逐梦之影"

}, {

	"ename": 152,

	"cname": "王昭君",

	"title": "冰雪之华",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "冰雪之华|精灵公主|偶像歌手|凤凰于飞|幻想奇妙夜"

}, {

	"ename": 153,

	"cname": "兰陵王",

	"title": "暗影刀锋",

	"new_type": 0,

	"hero_type": 4,

	"skin_name": "暗影刀锋|隐刃|暗隐猎兽者"

}, {

	"ename": 154,

	"cname": "花木兰",

	"title": "传说之刃",

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 4,

	"skin_name": "传说之刃|剑舞者|兔女郎|水晶猎龙者|青春决赛季|冠军飞将|瑞麟志"

}, {

	"ename": 156,

	"cname": "张良",

	"title": "言灵之书",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "言灵之书|天堂福音|一千零一夜|幽兰居士"

}, {

	"ename": 157,

	"cname": "不知火舞",

	"title": "明媚烈焰",

	"new_type": 0,

	"hero_type": 2,

	"hero_type2": 4,

	"skin_name": "明媚烈焰"

}, {

	"ename": 162,

	"cname": "娜可露露",

	"title": "鹰之守护",

	"new_type": 0,

	"hero_type": 4,

	"skin_name": "鹰之守护"

}, {

	"ename": 163,

	"cname": "橘右京",

	"title": "神梦一刀",

	"new_type": 0,

	"hero_type": 4,

	"hero_type2": 1,

	"skin_name": "神梦一刀"

}, {

	"ename": 166,

	"cname": "亚瑟",

	"title": "圣骑之力",

	"pay_type": 11,

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 3,

	"skin_name": "圣骑之力|死亡骑士|狮心王|心灵战警"

}, {

	"ename": 167,

	"cname": "孙悟空",

	"title": "齐天大圣",

	"new_type": 0,

	"hero_type": 4,

	"hero_type2": 1,

	"skin_name": "齐天大圣|地狱火|西部大镖客|美猴王|至尊宝|全息碎影|大圣娶亲"

}, {

	"ename": 168,

	"cname": "牛魔",

	"title": "精英酋长",

	"new_type": 0,

	"hero_type": 6,

	"hero_type2": 3,

	"skin_name": "精英酋长|西部大镖客|制霸全明星"

}, {

	"ename": 169,

	"cname": "后羿",

	"title": "半神之弓",

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "半神之弓|精灵王|阿尔法小队|辉光之辰|黄金射手座"

}, {

	"ename": 170,

	"cname": "刘备",

	"title": "仁德义枪",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "仁德义枪|万事如意|纽约教父|汉昭烈帝"

}, {

	"ename": 171,

	"cname": "张飞",

	"title": "禁血狂兽",

	"new_type": 0,

	"hero_type": 3,

	"hero_type2": 6,

	"skin_name": "禁血狂兽|五福同心|乱世虎臣"

}, {

	"ename": 173,

	"cname": "李元芳",

	"title": "王都密探",

	"pay_type": 10,

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "王都密探|特种部队|黑猫爱糖果|逐浪之夏"

}, {

	"ename": 174,

	"cname": "虞姬",

	"title": "森之风灵",

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "森之风灵|加勒比小姐|霸王别姬|凯尔特女王"

}, {

	"ename": 175,

	"cname": "钟馗",

    "title": "虚灵城判",

	"new_type": 0,

	"hero_type": 6,

	"hero_type2": 2,

	"skin_name": "虚灵城判|地府判官"

}, {

	"ename": 177,

	"cname": "成吉思汗",

	"title": "苍狼末裔",

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "苍狼末裔|维京掠夺者"

}, {

	"ename": 178,

	"cname": "杨戬",

	"title": "根源之目",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "根源之目|埃及法老|永曜之星"

}, {

	"ename": 183,

	"cname": "雅典娜",

	"title": "圣域余晖",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "圣域余晖|战争女神|冰冠公主|神奇女侠"

}, {

	"ename": 184,

	"cname": "蔡文姬",

	"title": "天籁弦音",

	"new_type": 0,

	"hero_type": 6,

	"skin_name": "天籁弦音|蔷薇王座|舞动绿茵|奇迹圣诞"

}, {

	"ename": 186,

	"cname": "太乙真人",

	"title": "炼金大师",

	"new_type": 0,

	"hero_type": 6,

	"hero_type2": 3,

	"skin_name": "炼金大师|圆桌骑士|饕餮|华丽摇滚"

}, {

	"ename": 180,

	"cname": "哪吒",

	"title": "桀骜炎枪",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "桀骜炎枪|三太子|逐梦之翼"

}, {

	"ename": 190,

	"cname": "诸葛亮",

	"title": "绝代智谋",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "绝代智谋|星航指挥官|黄金分割率|武陵仙君|掌控之力"

}, {

	"ename": 192,

	"cname": "黄忠",

	"title": "燃魂重炮",

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "燃魂重炮|芝加哥教父"

}, {

	"ename": 191,

	"cname": "大乔",

    "title": "沧海之曜",

	"new_type": 0,

	"hero_type": 6,

	"skin_name": "沧海之曜|伊势巫女|守护之力|猫狗日记"

}, {

	"ename": 187,

	"cname": "东皇太一",

    "title": "噬灭日蚀",

	"new_type": 0,

	"hero_type": 6,

	"hero_type2": 3,

	"skin_name": "噬灭日蚀|东海龙王|逐梦之光"

}, {

	"ename": 182,

	"cname": "干将莫邪",

	"title": "淬命双剑",

	"pay_type": 10,

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "淬命双剑|第七人偶|冰霜恋舞曲"

}, {

	"ename": 189,

	"cname": "鬼谷子",

	"title": "万物有灵",

	"new_type": 0,

	"hero_type": 6,

	"skin_name": "万物有灵|阿摩司公爵|幻乐之宴"

}, {

	"ename": 193,

	"cname": "铠",

	"title": "破灭刃锋",

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 3,

	"skin_name": "破灭刃锋|龙域领主|曙光守护者|青龙志"

}, {

	"ename": 196,

	"cname": "百里守约",

	"title": "静谧之眼",

	"new_type": 0,

	"hero_type": 5,

	"hero_type2": 4,

	"skin_name": "静谧之眼|绝影神枪|特工魅影|朱雀志"

}, {

	"ename": 195,

	"cname": "百里玄策",

	"title": "嚣狂之镰",

	"new_type": 0,

	"hero_type": 4,

	"skin_name": "嚣狂之镰|威尼斯狂欢|白虎志"

}, {

	"ename": 194,

	"cname": "苏烈",

	"title": "不屈铁壁",

	"new_type": 0,

	"hero_type": 3,

	"hero_type2": 1,

	"skin_name": "不屈铁壁|爱与和平|坚韧之力|玄武志"

}, {

	"ename": 198,

	"cname": "梦奇",

	"title": "入梦之灵",

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 3,

	"skin_name": "入梦之灵|美梦成真|胖达荣荣"

}, {

	"ename": 179,

	"cname": "女娲",

	"title": "至高创世",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "至高创世|尼罗河女神"

}, {

	"ename": 501,

	"cname": "明世隐",

	"title": "灵魂劫卜",

	"new_type": 0,

	"hero_type": 6,

	"skin_name": "灵魂劫卜|占星术士|虹云星官"

}, {

	"ename": 199,

	"cname": "公孙离",

	"title": "幻舞玲珑",

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "幻舞玲珑|花间舞|蜜橘之夏"

}, {

	"ename": 176,

	"cname": "杨玉环",

	"title": "霓裳风华",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "风华霓裳|霓裳曲|遇见飞天"

}, {

	"ename": 502,

	"cname": "裴擒虎",

	"title": "六合虎拳",

	"new_type": 0,

	"hero_type": 4,

	"hero_type2": 1,

	"skin_name": "六合虎拳|街头霸王|梅西"

}, {

	"ename": 197,

	"cname": "弈星",

	"title": "天元之弈",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "天元之弈|踏雪寻梅"

}, {

	"ename": 503,

	"cname": "狂铁",

	"title": "战车意志",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "战车意志|命运角斗场|御狮"

}, {

	"ename": 504,

	"cname": "米莱狄",

	"title": "筑城者",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "筑城者|精准探案法|御霄"

}, {

	"ename": 125,

	"cname": "元歌",

	"title": "无间傀儡",

	"new_type": 0,

	"hero_type": 4,

	"skin_name": "无间傀儡|午夜歌剧院"

}, {

	"ename": 510,

	"cname": "孙策",

	"title": "光明之海",

	"new_type": 0,

	"hero_type": 3,

	"hero_type2": 1,

	"skin_name": "光明之海|海之征途|猫狗日记"

}, {

	"ename": 137,

	"cname": "司马懿",

	"title": "寂灭之心",

	"pay_type": 10,

	"new_type": 0,

	"hero_type": 4,

	"hero_type2": 2,

	"skin_name": "寂灭之心|魇语军师"

}, {

	"ename": 509,

	"cname": "盾山",

	"title": "无尽之盾",

	"new_type": 0,

	"hero_type": 6,

	"hero_type2": 3,

	"skin_name": "无尽之盾|极冰防御线"

}, {

	"ename": 508,

	"cname": "伽罗",

	"title": "破魔之箭",

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "破魔之箭|花见巫女"

}, {

	"ename": 312,

	"cname": "沈梦溪",

	"title": "爆弹怪猫",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "爆弹怪猫|棒球奇才"

}, {

	"ename": 507,

	"cname": "李信",

	"title": "谋世之战",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "谋世之战|灼热之刃"

}, {

	"ename": 513,

	"cname": "上官婉儿",

	"title": "惊鸿之笔",

	"new_type": 0,

	"hero_type": 2,

	"hero_type2": 4,

	"skin_name": "惊鸿之笔|修竹墨客"

}, {

	"ename": 515,

	"cname": "嫦娥",

	"title": "寒月公主",

	"new_type": 0,

	"hero_type": 2,

	"hero_type2": 3,

	"skin_name": "寒月公主|露花倒影"

}, {

	"ename": 511,

	"cname": "猪八戒",

	"title": "无忧猛士",

	"new_type": 0,

	"hero_type": 3,

	"skin_name": "无忧猛士|年年有余"

}, {

	"ename": 529,

	"cname": "盘古",

	"title": "破晓之神",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "破晓之神"

}, {

	"ename": 505,

	"cname": "瑶",

	"title": "鹿灵守心",

	"pay_type": 10,

	"new_type": 0,

	"hero_type": 6,

	"skin_name": "森"

}, {

	"ename": 506,

	"cname": "云中君",

	"pay_type": 10,

    "title": "流云之翼",

	"new_type": 0,

	"hero_type": 4,

	"hero_type2": 1,

	"skin_name": "荷鲁斯之眼"

}, {

	"ename": 522,

	"cname": "曜",

	"title": "星辰之子",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "归虚梦演"

}, {

	"ename": 518,

	"cname": "马超",

	"title": "冷晖之枪",

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 4

}, {

	"ename": 523,

	"cname": "西施",

	"title": "幻纱之灵",

	"new_type": 0,

	"hero_type": 2,

	"skin_name": "幻纱之灵|归虚梦演"

}, {

	"ename": 525,

	"cname": "鲁班大师",

	"title": "神匠",

	"new_type": 0,

	"hero_type": 6,

	"skin_name": "神匠|归虚梦演"

}, {

	"ename": 524,

	"cname": "蒙犽",

	"title": "烈炮小子",

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "烈炮小子|归虚梦演"

}, {

	"ename": 531,

	"cname": "镜",

	"title": "破镜之刃",

	"new_type": 0,

	"hero_type": 4,

	"skin_name": "破镜之刃|冰刃幻境"

}, {

	"ename": 527,

	"cname": "蒙恬",

	"title": "秩序统将",

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 3,

	"skin_name": "秩序统将|秩序猎龙将"

}, {

	"ename": 533,

	"cname": "阿古朵",

	"title": "山林之子",

	"new_type": 0,

	"hero_type": 3,

	"hero_type2": 6,

	"skin_name": "山林之子"

},{

	"ename": 536,

	"cname": "夏洛特",

	"title": "玫瑰剑士",

	"new_type": 0,

	"hero_type": 1,

	"skin_name": "玫瑰剑士"

},{

	"ename": 528,

	"cname": "澜",

	"title": "鲨之猎刃",

	"new_type": 0,

	"hero_type": 4,

	"skin_name": "鲨之猎刃|孤猎"

},{

	"ename": 537,

	"cname": "司空震",

	"title": "雷霆之王",

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 2,

	"skin_name": "雷霆之王|启蛰"

},{

	"ename": 155,

	"cname": "艾琳",

	"title": "精灵之舞",

	"new_type": 0,

	"hero_type": 5,

	"skin_name": "精灵之舞|女武神"

},{

	"ename": 538,

	"cname": "云缨",

	"title": "燎原之心",

	"pay_type": 10,

	"new_type": 0,

	"hero_type": 1,

	"hero_type2": 4,

	"skin_name": "燎原之心|赤焰之缨"

},{

	"ename": 540,

	"cname": "金蝉",

	"title": "渡世行者",

	"new_type": 0,

	"hero_type": 2,

	"hero_type2": 6,

	"skin_name": "渡世行者|前尘"

},{

	"ename": 542,

	"cname": "暃",

	"title": "玉城之子",

	"new_type": 1,

	"hero_type": 4,

	"skin_name": "玉城之子|碧珀绯影"}]
import json

f = open('./hero.json', 'r', encoding="utf-8")
content = json.loads(f.read())

# print(content)
# for i in content:
#     print(i)

# 提取出每个英雄的皮肤
"""
[
    {"廉颇":['正义爆轰','地狱岩魂']},
    {"小乔":['恋之微风','万圣前夜'...]},
。。。。
]

"""
# {'ename': 542, 'cname': '暃', 'title': '玉城之子', 'new_type': 1, 'hero_type': 4, 'skin_name': '玉城之子|碧珀绯影'}

new_skin_name = [i.get('skin_name') for i in content]

# for i in content:
#     lst_cname.append(i.get('cname'))
lst_cname = [i.get('cname') for i in content]

# for j in new_skin_name:
#     lst_skin_name.append(str(j).split('|'))
lst_skin_name = [str(i).split('|') for i in new_skin_name]

dic = {}
for i in range(len(lst_cname)):
    dic[lst_cname[i]] = lst_skin_name[i]

# for k, v in dic.items():
#     lst.append({k: v})
lst = [{k: v} for k, v in dic.items()]

print(lst)

day3

1. 百度产品爬取

# 导包
import requests

# 1. 确定url
base_url = 'https://www.baidu.com/more/'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                         'Chrome/100.0.4896.88 Safari/537.36',
           }

# 2. 发送请求,获取响应
# 如果是get请求就是 .get() ,如果是post请求,就用 .post()
response = requests.get(url=base_url, headers=headers)
# print(requests) # <Response [200]>  -- 200表示成功了

# 3. 获取响应内容
# 3.1: response.text: 获取字符串格式的响应内容
# 解决乱码:
response.encoding = 'utf-8'

html_str = response.text
# print(html_str)

# 3.2二进制格式的响应正文:response.content -- 先记忆一下,后面的爬取案例会用到。(图片,视频,音乐)都需要使用这个
# print(response.content)


# 4. 保存到本地
with open('./baidu.html', mode='w', encoding='utf-8') as fp:
    fp.write(html_str)

2. 新浪新闻

import requests

# 1. 确定url


base_url = 'https://search.sina.com.cn/news'

# 1.1 封装请求头
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                         'Chrome/100.0.4896.88 Safari/537.36',
           }

# 1.2 封装请求数据 -- post 向服务器提交的数据
data = {
    'q': 'python',  # 搜索关键字
    'c': 'news',
    'range': 'all',
    'size': '7',
    'page': '3'  # 页码
}

# 2. 发送请求,获取相应
response = requests.get(url=base_url, headers=headers, data=data)

# 3. 获取响应内容
response.encoding = 'utf-8'
html_str = response.text
# print(html_str)  # 产看是否乱码

# 4. 保存数据
with open('./新浪新闻.html', mode='w', encoding='utf-8') as fp:
    fp.write(html_str)

3. 百度翻译

import requests


def translate(word):
    # 1.
    base_url = 'https://fanyi.baidu.com/sug'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/100.0.4896.88 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',

    }
    data = {
        'kw': word
    }

    # 2.
    response = requests.post(url=base_url, headers=headers, data=data)
    json_data = response.json()
    result = ''
    for data in json_data['data']:
        result += data['v'] + '\n'
    print(result)


if __name__ == '__main__':
    word = input("请输入单词:")
    translate(word)

4. 实现登录

import requests

# 1.
base_url = 'https://www.gsdata.cn/rank/toparc?wxname=eQmBhDhSbJmqhihnbgz5Y221OAO0O0OO0O0O&wx=zhanhao668&sort=-1'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/100.0.4896.88 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
    'Cookie': '53gid0=10401715626017; 53gid2=10401715626017; 53gid1=10401715626017; visitor_type=old; 53uvid=1; onliner_zdfq72213613=0; sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22180c79036a08a9-0232018b8da90d2-1734337f-1327104-180c79036a1962%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%22180c79036a08a9-0232018b8da90d2-1734337f-1327104-180c79036a1962%22%7D; _gsdataCL=WzAsIjE5ODYyMTYxOTU3IiwiMjAyMjA1MTUxOTUyNTIiLCI2NjFlYjMyODU2NDI0ZWUwZDAyZDdlYmU0ZDY5MzAwNiIsNDQ3ODY1XQ%3D%3D; _gsdataOL=447865%3B19862161957%3B%7B%220%22%3A%22%22%2C%221%22%3A%22%22%2C%222%22%3A%22%22%2C%223%22%3A%22%22%2C%224%22%3A%22%22%2C%225%22%3A%22%22%2C%226%22%3A%22%22%2C%227%22%3A%22%22%2C%228%22%3A%22%22%2C%229%22%3A%22%22%2C%2299%22%3A%2220220515%22%7D%3B68c776cba5faa7c2f2fcf7aa5a44c0b8; _identity-frontend=b81e9fa61269371d1ff59116135e10faeec62bc689dadb76554e66ca20a434e7a%3A2%3A%7Bi%3A0%3Bs%3A18%3A%22_identity-frontend%22%3Bi%3A1%3Bs%3A28%3A%22%5B%22734918%22%2C%22test+key%22%2C604800%5D%22%3B%7D; 53revisit=1652615579659; acw_tc=2f61f27b16526241269134619e6a3b75dd413298e3a8d9cfb339259afff306; PHPSESSID=dk1s9g3l2pqadolkjnv6ff2040; _csrf-frontend=28532c5e17d7b5ba0412c91f7d33dbe68a19661566b416160f147feb28b1cb18a%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%228mRhb-pE4w7f-PiGvdfiQ9tdqsn74InL%22%3B%7D; Hm_lvt_293b2731d4897253b117bb45d9bb7023=1652615559,1652624134; Hm_lpvt_293b2731d4897253b117bb45d9bb7023=1652624134; 53kf_72213613_from_host=www.gsdata.cn; 53kf_72213613_keyword=https%3A%2F%2Fu2.gsdata.cn%2F; 53kf_72213613_land_page=https%253A%252F%252Fwww.gsdata.cn%252Frank%252Fwxdetail%253Fwxname%253DeQmBhDhSbJmqhihnbgz5Y221OAO0O0OO0O0O; kf_72213613_land_page_ok=1',

}
data = {
    'wxname': 'eQmBhDhSbJmqhihnbgz5Y221OAO0O0OO0O0O',
    'wx': 'zhanhao668',
    'sort': '-1'
}

response = requests.post(url=base_url, headers=headers, data=data)

print(response.json()['data'])
# for data in response.json()['data']:
#     print(data['title'], data['content'])


补充重点 —> 正则表达式

1. 正则表达式

import re

string = 'one1two2three3'

# 1. 将正则表达式编译成一个patten对象
# 元字符:表示1
# /d:匹配出一个数字
pattern = re.compile(r'\d')

# 2.
#    search(): 匹配一个元素
m = pattern.search(string)
print(m)  # <re.Match object; span=(3, 4), match='1'>

# 3. 调用match 对象的group获取匹配到的内容
print(m.group())  # 1

#     findall
res = pattern.findall(string)
print(res)  # ['1', '2', '3']

#     sub: 替换
# 需求:将1个数组替换成 =
new_str = pattern.sub('=', string)
print(new_str)

2. 正则表达式的写法

import re

# (1)11位的电话号码
# p = re.compile(r'1[3-9]\d\d\d\d\d\d\d\d\d')
p = re.compile(r'1[3-9]\d{9}')
phone = 'asdfghjkl19862161957asdfghjkl'
res = p.search(phone)
print(res.group())

# 2.
nums = [-10, -9.8, 0, 11, 22, 1.9, 67, 100, -8, -111, 11.1, 1, 10]
for num in nums:
    num = str(num)
    # (1) 非负整数
    # * :重复零次或多次 ---- >= 0
    # + :重复一次或多次 ---- >= 1
    p = re.compile(r'^\d+$')

    # (2) 匹配正整数
    p = re.compile(r'^[1-9]\d*$')  # 1 位数可以匹配
    p = re.compile(r'^[1-9]\d+$')  # 不能匹配 1 位数

    # (3)非正整数
    p = re.compile(r'^[^1-9]\d*$')
    # (4)非负浮点数
    # (5)整数

    res = p.search(num)
    print(res, num)

# ()长度为8-10的用户密码(以字母开、包括数字、下划线)
p = re.compile(r'^[a-zA-Z]\w{7,9}$')

# ()验证qq邮箱
p = re.compile(r'[1-9]\d{4,}@qq\.com')

# ()匹配出wahaer,banaer,qqxier 内容
# 正则表达式的写法:一位一位的限定
string = 'wahaer seafaf qqxier fawfaf banaer wefafa wawqafa'
p = re.compile(r'[wbq][aq][hnx][ai]er')
res = p.findall(string)
print(res)  # ['wahaer', 'qqxier', 'banaer']

3. 正则的万能写法

import re

string = '1957年12月,李家超出生于香港一个基层家庭,小学就读深水埗五邑工商总会学校,中学就读于九龙华仁书院,成绩优异。'

# .*? 配合re.S
# . 除换行符以外的任意字符
# *:. 字符重复 >= 0 次
#  ?: 非贪婪模式
# re.S 可以让.匹配换行

p = re.compile(r'^(.*?),李家', re.S)
m1 = p.search(string).group()  # 1957年12月,李家
print(m1)

m2 = p.search(string).group(1)  # 1 表示 1分组,获取第一个括号中匹配的内容
print(m2)   # 1957年12月

p = re.compile(r'就读(.*?),中学', re.S)
m3 = p.search(string).group(1)
print(m3)

# 一次性将 日期,小学,中学都取出来
p = re.compile(r'^(.*?),李家.*就读(.*?),中学就读于(.*?),成')
print(p.search(string).group(1))
print(p.search(string).group(2))
print(p.search(string).group(3))

day4

重要案例 —> 把得到的数据写道excel中

# 所有需要存储的数据
data = []


def write_to_excel(file_name):
    """
    将数据保存到excel中
    @:param: file_name 表示文件名
    :return:
    """
    # 1. 准备列索引:(excel的表头)
    head = list(data[0].keys())
    # 2. 准备数据部分
    excel_data = []
    for item in data:
        excel_data.append(list(item.values()))
    import pandas
    pandas.DataFrame(
        columns=head,  # 准备的列索引
        data=excel_data,  # 准备的数据部分
    ).to_excel(
        excel_writer=file_name,  # 保存的文件名
        index=False,  # 不保存列索引
    )


1. 股吧案例

import requests
import re

# 所有需要存储的数据
data = []


def write_to_excel(file_name):
    """
    将数据保存到excel中
    @:param: file_name 表示文件名
    :return:
    """
    # 1. 准备列索引:(excel的表头)
    head = list(data[0].keys())
    # 2. 准备数据部分
    excel_data = []
    for item in data:
        excel_data.append(list(item.values()))
    import pandas
    pandas.DataFrame(
        columns=head,  # 准备的列索引
        data=excel_data,  # 准备的数据部分
    ).to_excel(
        excel_writer=file_name,  # 保存的文件名
        index=False,  # 不保存列索引
    )


# 1确定url
# 找分页的规律
# 找请数据的url具体是那一条。
# %s 来做拼接
base_url = 'https://guba.eastmoney.com/default,99_%s.html'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36'
}

# 2. 发送请求,获取相应
# for i in range(1, 2):
for i in range(1, 13):
    url = base_url % i  # i即为 %s 要插入的数据
    response = requests.get(url=url, headers=headers)
    # print(response.text)

    # 3.提取数据
    # 使用正则的方式提取请求数据
    # 正则提取方法:一步一步缩小匹配的范围,做到精准匹配
    # 1. 提取url
    ul_p = re.compile(r'<ul class="newlist" tracker-eventcode="gb_xgbsy_ lbqy_rmlbdj">(.*?)</ul>', re.S)
    ul_c = ul_p.search(response.text).group(1)
    # print(ul_c)

    # 第二步:提取出所有的li--findall
    li_p = re.compile(r'<li>(.*?)</li>', re.S)
    li_list = li_p.findall(ul_c)
    # print(li_list)

    # 第三步:遍历上一步提取list,从每个li中提取每条数据。
    for li in li_list:
        nums = re.compile(r'<cite>(.*?)</cite>', re.S).findall(li)
        # print(nums)

        # 阅读数
        read_num = nums[0].strip()  # strip() 去掉字段前后的空格
        # 评论数
        comment_num = nums[1].strip()
        # print(read_num, comment_num)

        # 标题
        title = re.compile(r'" title="(.*?)" class="note">', re.S).search(li).group(1)
        # print(title)

        # 作者
        author = re.compile(r'target="_blank"><font>(.*?)</font></a><input', re.S).search(li).group(1)
        # print(author)

        # 更新时间
        last_date = re.compile(r'<cite class="last">(.*?)</cite>').search(li).group(1)
        # print(last_date)

        # 提取到的数据分分装到字典中
        item = {}
        item['read_num'] = read_num
        item['comment_num'] = comment_num
        item['title'] = title
        item['author'] = author
        item['last_date'] = last_date
        # print(item)

        # 将提取到的每一条数据添加到全局变量中
        data.append(item)

# 4. 保存数据
write_to_excel(file_name='股吧最新咨询数据.xlsx')


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值