0
点赞
收藏
分享

微信扫一扫

学习 Python 05天

微言记 2022-02-28 阅读 57

01-字符串操作

# 1、切分字符串
language = "Python and Java and C++ and Golang and Scala"
# split 切割字符串 生成一个列表: 暂时理解为一个容器 "有序序列"
result1 = language.split("and")
print(result1)

# 2、链接序列 生成字符串 跟 split 是相反操作
lang = ["English","Chinese"]
# 通过 - 连接上面的字符  形成字符串
result2 = "-".join(lang)
print(result2, type(result2))

# 3、删除字符串两边的空格 strip
class_name = "  Big Data "
print(len(class_name))
# 删除两边空格
class_name_new = class_name.strip()
print(class_name_new,len(class_name_new))

# 4、判断一个字符串是否以指定字串开始
mystr = "hello world"
# mystr 以hello开始,则为真
print(mystr.startswith("hello"))
# 以 world开头则返回False
print(mystr.startswith("world"))
# 以world结束则返回True
print(mystr.endswith("world"))
# 判断在指定范围内是否以hello开始
print(mystr.startswith("hello", 3, 8))
# 判断在指定范围内是否以lo开始
print(mystr.startswith("lo", 3, 8))

02-列表操作

# 列表[], 然后里面可以是任何类型的数据 12, 23.6, "",[]

# 列表本质上是一个序列 0        1      2        3
name_list = ["James", "Peter", "Bob", "Green", "Google"]
print(name_list, type(name_list), len(name_list))
# 1、列表索引查找
print(name_list[0])
print(name_list[1])
print(name_list[2])
print(name_list[3])
print(name_list[4])
print("==================================================")


# 使用index查找指定的数据 返回指定数据在列表 中的位置
print(name_list.index("Green"))

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

# 在指定的列表范围内查找Green 没有找到 则报错
# print(name_list.index("Green", 0, 2))

name_list2 = ["Chunk", "Brain", "Stonking", "Brain"]
result1 = name_list2.count("Brain")
result2 = name_list2.count("Chunk")
result3 = name_list2.count("Linda")
print(result1, result2, result3)

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

# 3、计算列表长度
print(len(name_list))
print(len(name_list2))

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

# 4、 判断指定元素是否存在
name_list3 = ["James", "Peter", "Bob", "Green", "Google"]
print("James" in name_list3)
print("Chunk" in name_list3)
print("Brain" not in name_list3)
print("Green" not in name_list3)

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

# 5、增加一个元素到列表中 加到列表末尾
name_list3.append("Linda")
print(name_list3)

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

# 追加一个序列 将一个列表整体加入到列表中
name_list3.append(["Link", "Will"])
print(name_list3)

# 追加一个序列 将序列中的值一个一个加入进去
name_list3.extend(["Nations", "Weak"])
print(name_list3)

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

03-删除列表以及列表排序

# 1、删除列表
name_list1 = ["刘玄德", "关云长", "张翼德"]
print("删除前:", name_list1)
del name_list1
# 删除之后name_list1 不存在 报错
# print("删除后:", name_list1)

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

# 删除列表中的指定下标的元素
#                0        1        2        3
name_list2 = ["孙悟空", "唐玄奘", "沙悟净", "猪悟能"]
del name_list2[1]
print(name_list2)

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

# 删除掉指定下标的元素 然后返回该元素
result1 = name_list2.pop(1)
print(name_list2)
print(result1)

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

# pop里面没有参数 则默认删除列表中的最后一个元素 然后返回该元素
name_list3 = ["东方不败", "令狐冲", "楚留香"]
result2 = name_list3.pop()
print(name_list3)
print(result2)

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

# remove删除指定元素 没有返回值
name_list4 = ["A", "B", "C"]
name_list4.remove("B")
print(name_list4)

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

# 清空列表 没有返回值
name_list4.clear()
print(name_list4)

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

# 2、修改列表    0    1    2
name_list5 = ["Q", "W", "E"]
name_list5[0] = "R"
print(name_list5)

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

# 3、列表翻转 没有返回值
name_list5.reverse()
print(name_list5)

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

# 4、列表排序 默认是从小到大
score_list = [35, 58, 77, 0]
score_list.sort()
print(score_list)

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

# 从大到小进行排序
score_list.sort(reverse=True)
print(score_list)

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

# 5、复制列表
height_list = [183, 155, 185, 145]
height_list_new = height_list.copy()
print("新的复制列表:", height_list_new)
print("原来的列表", height_list)

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

04-列表循环

# while 循环列表

number_list = ["123", "532", "797"]

i = 0
while i < 3:
    print(i, number_list[i])
    i += 1

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

# for循环 循环列表
number_list2 = [153, 593, 487, 493]
# 通过j这个临时变量 挨个地取列表中取数据 从头到尾 没有更多数据后结束
for j in number_list2:
    print(j)

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

05-列表嵌套

        

# 列表嵌套
import random

list1 = [[12,23], [34,45], 56, 78]
print(list1[0])
# 单独把23提取出来
print(list1[0][1])
list1[0].append(55)
print(list1)

'''
练习一
办公室有3个,8位老师要过来,随机分配办公室
打印出办公室有的老师
'''
#         0   1   2
office = [[], [], []]
teacher = ["A", "B", "C", "D", "E", "F", "G", "H"]
i = 0
while i < 8:
    office[random.randint(0, 2)].extend(teacher[i])
    i += 1
print(office)

'''
练习2
相亲:要求身高185cm,200万,8分,才是合格对象
资产1000万以上直接成功
'''
boy_list = [
    ["Peter", "181cm", "200万", "6分"],
    ["Harry", "172cm", "150万", "5分"],
    ["Bob", "138cm", "1500万", "3分"]
]
height = "186cm"
height_new = int(height[:-2])
print(height_new)
money = "200万"
money_new = int(money[:-1])
print(money_new)
score = "8分"
score_new = int(score[:-1])
print(score_new)

i = 0
while i < 3:
    if int(boy_list[i][1][:-2]) > height_new and int(boy_list[i][2][:-1]) > money_new and int(boy_list[i][3][:-1]) > score_new:
        print(boy_list[i][0], "相亲完成")
    elif int(boy_list[i][2][:-1]) > 1000:
        print(boy_list[i][0], "相亲完成")
    else:
        print(boy_list[i][0],"相亲失败")
    i += 1
举报

相关推荐

0 条评论