字符串操作
language = "Python and Java and C++ and Golang and Scala"
result1 = language.split("and")
print(result1)
lang = ["English", "Chinese", "Jananese"]
result2 = "-".join(lang)
print(result2, type(result2))
class_name = " Big Data "
print(len(class_name))
class_name_new = class_name.strip()
print(class_name_new, len(class_name_new))
mystr = "hello world"
print(mystr.startswith("hello"))
print(mystr.startswith("world"))
print(mystr.endswith("world"))
print(mystr.startswith("hello", 3, 8))
print(mystr.startswith("lo", 3, 8))
列表操作1
name_list = ["James", "蔡徐坤", "罗志祥", "格林", 2022]
print(name_list, type(name_list), len(name_list))
print(name_list[0])
print(name_list[1])
print(name_list[3])
print(name_list[2])
print(name_list[4])
print(name_list.index("格林"))
name_list2 = ["蒋卢", "吴苹雨", "李龙波", "蒋卢"]
result1 = name_list2.count("蒋卢")
result2 = name_list2.count("李龙波")
result3 = name_list2.count("饶鹏鹏")
print(result1, result2, result3)
print(len(name_list))
print(len(name_list2))
name_list3 = ["***", "***", "****", "***"]
print("***" in name_list3)
print("***" in name_list3)
print("***" not in name_list3)
print("***" not in name_list3)
name_list3.append("***")
print(name_list3)
name_list3.append(["***", "***"])
print(name_list3)
name_list3.extend(["***", "***"])
print(name_list3)
name_list3.insert(1, "****")
print(name_list3)
列表操作2
name_list1 = ["张飞", "关羽", "刘备"]
print("删除前:", name_list1)
del name_list1
name_list2 = ["孙悟空", "唐僧", "八戒", "沙僧"]
del name_list2[1]
print(name_list2)
result1 = name_list2.pop(1)
print(name_list2)
print(result1)
name_list3 = ["帅帅", "东东", "根根"]
result2 = name_list3.pop()
print(result2)
print(name_list3)
name_list4 = ["田田", "豪豪", "浩浩"]
name_list4.remove("豪豪")
print(name_list4)
name_list4.clear()
print(name_list4)
name_list5 = ["孝孝", "昊昊", "冯婕小仙女"]
name_list5[0] = "荣荣"
print(name_list5)
name_list5.reverse()
print(name_list5)
score_list = [35, 89, 77, 0]
score_list.sort()
print(score_list)
score_list.sort(reverse=True)
print(score_list)
height_list = [183, 155, 185, 145]
height_list_new = height_list.copy()
print("新的复制列表:", height_list_new)
print("原来的列表:", height_list)
列表循环
country_list = ["乌克兰", "俄罗斯", "漂亮国", "中国"]
i = 0
while i < len(country_list):
print(i, country_list[i])
i += 1
print("=========================================")
scenery_list = ["船舶大楼", "毛家屋场", "白鹿寺", "秀峰公园"]
for j in scenery_list:
print(j)
列表嵌套
name_list = [["宏宏", "伟伟"], ["天天", "顺顺"], "廖警官"]
print(name_list[0])
print(name_list[0][1])
name_list[0].append("亮亮")
print(name_list)
'''
练习1:
某学校有3个办公室,但是有8位老师要过来,随机分配办公室
最后打印出 办公室有的老师
'''
office = [[], [], []]
teacher = ["A", "B", "C", "D", "E", "F", "G", "H"]
'''
练习2:某某某相亲:要求身高185cm,200万,8分,才是合格对象
资产1000万以上 直接相亲成功
'''
boy_list = [
["吴豪", "181cm", "200万", "6分"],
["吕浩", "172cm", "150万", "5分"],
["吕晓峰", "138cm", "1500万", "3分"]
]
height = "186cm"
height_new = int(height[:-2])
print(height_new, type(height_new))