列表 元组 字典 及 相关函数

# """
# 列表  []
# 可进行 截取 索引 组合
#
# """
#
# list = ["Goolge", "Runoob", 1997, 2000]
# list2 = [1, 2, 3, 4, 5, 6]
#
# print(list[0])
# print(list2[1:5])
# print(list[:2])
# list[2] = "biyunsheng"
# print(list)
# del list[3]
# print(list)
#
# # 列表脚本操作
#
# print(len(list))  #长度
# print(list + list2)  # 组合
# print(list*3)         #重复
# print(1 in list2)     # 存在
# for x in list2:       # 迭代
#     print(x, end="")
# # 列表截取与拼接
# print()
# list = ["goole", "runoob", "taobao"]
#
# print(list[-1])
# print(list + [1, 2, 3])
#
# #嵌套
# list = [[1, 2], [1, 2, 3]]     #打印列表每一个bianliang
# print(list)
# print(list[0][1])
# for i in range(len(list)):
#     for x in range(len(list[i])):
#         print(list[i][x],end="")
#
# print()

#
# # 将元组转化为列表
# tupe = (1, 2, 3, 4)
# print(list(tupe))
# # #列表函数
# # list = [1, 2, 2, 3, 4]
# #
# # len(list)
# # max(list)
# # min(list)
# #
# # list.append("wangzhe")  #在列表末尾添加新对象
# # print(list)
# #
# list1 = [1, 2, 3, 4]
# list1.append("jsfkljd")
# print(list1)
# # a = list.count(2)       # 统计某个元素在列表中出现的次数
# # print(a)
# print(list1.count(3))
# # list = [1, 2, 3]    # 在列表末尾添加多个对象
# # new = [1, 2]
# # list.extend(new)
# # print(list)
#
# list2 = [1, 2, 3, 4, 5]
# list2.extend([1, 2, 3, 4, "ah"])
# print(list2)
# #
# # print(list.index(2))  # 从列表中找出某个值第一个匹配的索引位置
# #
# list3 = [1, 2, 3, 4, 5]
# print(list3.index(3))
# # list.insert(0, 1)      #  将对象插入列表  list.insert(位置,对象)
# list4 = [1, 2, 3, 4, 5]
# list4.insert(0, "abc")
# print(list4)
# # print(list)
# #
# # list.pop()
# # print(list.pop())   #  移除列表中的一个元素 ,并返回该元素的值 默认是最后一个元素
# #                      # 将某个位置中的元素拿出来  并返回数值  list.pop(位置)
# # print(list)
# #
# list5 = [1, 2, 3, 4, 5]
# print(list5.pop(0))
#
#
# # list.remove(2)       #移除列表中的第一个 匹配值
# # print(list)
# list6 = [1, 2, "abc", 3, 4, 5]
# print("移除前", list6)
# list6.remove("abc")
# print("移除后", list6)
#
# # list.reverse()     #反向列表
# # print(list)
# list7 = [1, 2, "abc", 3, 4, 5]
# print("反向前", list7)
# list7.reverse()
# print("反向后", list7)
#
#
# # list = [2, 3, 4, 2, 5, 1, 6, 5, 3, 0]
# # list.sort()
# # print("排序", list)
# list8 = [1, 2, 3, 4, 5]
# list8.sort()
# print("排序后", list8)
#
#
#
# # a = list.copy()   # 复制列表
# # print(a)
# """
# list8 = [1, 2, 3, 4, 5]
# list9 = ["a", "b", "c"]
# list9 = list8
# list9[0] = "abc"
# print(list8)
# print(list9)
#
# 输出结果
# ['abc', 2, 3, 4, 5]
# ['abc', 2, 3, 4, 5]
#
# """
# list8 = [1, 2, 3, 4, 5]
# list9 = ["a", "b", "c"]
# list9 = list8.copy()
# list9[0] = "abc"
# print(list8)
# print(list9)
#
# list8 = [1, 2, 5, 4, 5]
# for x in list8.copy():
#     if x == 5:
#         list8.remove(x)
# print(list8)
#
# # list.clear()  #清空列表
# # print(list)
# #
#
# # print()
# # print(list9.clear())

# ##元组   ***不可修改*** 但可以运算
# print("元组")
#
# tup1 = ()
#

#创建一个列表 放6个名字
# tuple1 = ["毕云胜", "张三", "刘四", "王刚", "刘鹏", "王五"]
#
# for Name in tuple1.copy():
#     print(Name, end=" ")
# print()
#
# for Name in tuple1.copy():
#     if Name[0] == "刘":
#         continue
#     print(Name)

#print(4)
# for Name in tuple1.copy():
#     if "刚" in Name:
#         break
#print(tuple1)

# print(5)
#
# tuple1 = ["毕云胜", "张三", "刘四", "王刚", "刘鹏", "王五"]
# isFind = False
# for name in tuple1.copy():
#     if name == "张三":
#         tuple1.remove("张三")
#         isFind = True
# if isFind == False:
#     tuple1.append("张三")
#     print("没有找到张三")
#
# # ### 字典   值可以变 但是 键值必须不可变且必须是唯一的 如数字 字符串 元组
# zi = {"ch":"中国", "us":"美国", "fr":"法国"}
#
# print(zi["ch"])  #  根据键值找到数值value 如果key不存在 则报错
#
# zi["键"] = "123"
# print(zi)


# d = {"key1": "valuel", "key2": "value2"}
#
# d["key1"] = "1234"          # 更新元素
#根据key 修改value
# print("更新的元素",  d["key1"])


# d["aa"] = "这个可以吗?"  #  添加信息
# print("添加的信息", d["aa"])
# print(d)
#
# ###删除字典元素  直接删除键值
# d = {"key1": "valuel", "key2": "value2"}
# del d["key1"]
# print(d)
#
# d.clear()   #清空字典
# print(d)
# zi = {"ch":"中国", "us":"美国", "fr":"法国"}
# zi.clear()
# print(zi)

# del d  # 删除字典
#
# zi = {"ch":"中国", "us":"美国", "fr":"法国"}
# del zi

# zi = {"ch":"中国", "us":"美国", "fr":"法国"}
#
# #删除键值对
# del zi["ch"]
# print(zi)


# d = {1:"hello", 2:"world"}
# d.setdefault("字典")   # 根据键值查找 如果没有则添加新的键值对
# print(d)
#
# zi = {"ch":"中国", "us":"美国", "fr":"法国"}
# zi.setdefault("ao")
# print(zi)
#
# zi.get("ch")   #根据键值查找 , 如果没有则返回None
# print("aa")

# dd = d
# ###字典方法
# zi = {"ch":"中国", "us":"美国", "fr":"法国"}
# a = d.keys()    #得到字典所有键值

# print(zi.keys())
# print(zi.values())
# print(zi.items())

# b = d.values()  #得到字典所有值
#
# c = d.items()    #得到字典所有键值对
#
# zi = {"ch":"中国", "us":"美国", "fr":"法国"}
#
# for x, y in zi.items():
#     print(x, ":", y)
# d.clear()      # 清空键值
#
# zi.clear()
# print(zi)

###赋值变量  多个变量赋值 数量要相对应
a, b, c, d= 1, 2, 3, 4
print(a, b, c, d)

# list = [1, 2, 3, 4]
# it = iter(list)      # 创建代迭器对象
# print(next(it))      # 输出迭代器的下一个元素
# print(next(it))
# print(next(it))
# print(next(it))


# ##   迭代器遍历
# list = [1, 2, 3, 4]
# it = iter(list)
# for x in it:
#     print(x, end="")
#
# list = [1, 2, 3, 4]
#


st ={"张三": 18, "李四": 19, "王五": 20, "刘六": 10, "wang": 15}

print(st.keys())
print(st.values())
for x in st.keys():
    st[x] += 2
print(st)

print(st.keys())
# 创建列表


# 20.list3中找出姓“刘的同学”并把他们的姓名倒序输出,找不到给出提示
list3 = ['1809班王1', '1809班刘2', '1809班王3', '1809班马4', '1809班毕5', '1808班张三', '1808班王四', '1808班张五', '1808班马刚', '1808班毕七']
panduan = True
for x in range(len(list3)):
    a = list(list3[x])
    if a[5] == "刘":
        a = a[5:]
        a.reverse()
        b = "".join(a)   # list  转换为  string
        print(b)
        panduan =False
if panduan:
    print("该列表中没有姓刘的")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值