西安1期-杨鹿 发表于 2022-4-14 21:54:42

4.14作业

定义一个列表
# list1 = ["shanxi","yulin","test",800820]
# print(list1)
# ['shanxi', 'yulin', 'test', 800820]
# print(type(list1))数据类型为列表
# 第二种方法:
# str1="shanxi"
# list1 = list(str1)
# print(list1)['s', 'h', 'a', 'n', 'x', 'i']
# print(type(list1))<class 'list'>
# 索引赋值:
# list1 = ["shanxi","yulin","test",800820]
# list1 = "jingbian" 把字符jingbian赋值到列表2的位置
# print(list1)
# ['shanxi', 'yulin', 'jingbian', 800820]
# 切片赋值:
# list1 = ["shanxi","yulin","test",800820]
# list1 = "sos"
# print(list1)
# ['shanxi', 'yulin', 's', 'o', 's']

# append函数在列表末尾添加任何数据类型元素
# list1 = ["shanxi","yulin","test",800820]
# list1.append("yuan")
# print(list1)添加str
# ['shanxi', 'yulin', 'test', 800820, 'yuan']
# list1.append(1234)
# print(list1)添加int
# ['shanxi', 'yulin', 'test', 800820, 1234]

# extend函数,连接两个列表,变成一个列表,且把元素合到一个列表中
# 不能与int进行整合
# list1 = ["shanxi","yulin","test",800820]
# list2 = ["nihaoya"]
# list1.extend(list2)
# print(list1)
# ['shanxi', 'yulin', 'test', 800820, 'nihaoya']

# insert函数,根据索引位置去添加元素到列表中
# list1 = ["shanxi","yulin","test",800820]
# list1.insert(2,"halou")
# print(list1)添加str字符串
# ['shanxi', 'yulin', 'halou', 'test', 800820]
# list1.insert(0,"666")
# print(list1)添加整形int
# ['666', 'shanxi', 'yulin', 'test', 800820]

# index函数,查看列表元素的索引值
# list1 = ["shanxi","yulin","test",800820]
# haha=list1.index("yulin")
# print(haha):1

# remove函数,移除列表中的元素,根据value值删除
# list1 = ["shanxi","yulin","test",800820]
# list1.remove("test")删除“test
# print(list1)
# ['shanxi', 'yulin', 800820]

# del,是一个关键字,根据索引进行删除列表元素
# list1 = ["shanxi","yulin","test",800820]
# del list1
# print(list1)索引删除
# ['shanxi', 'yulin', 'test']
# del list1
# print(list1)切片删除
# ['shanxi', 'yulin', 'test']

# sort()对列表进行排序
# list1 = ["shanxi","yulin","test","800820"]
# list1.sort()
# print(list1)str字符串
# ['800820', 'shanxi', 'test', 'yulin']
# list1 = ["Shanxi","Yulin","test","800820"]
# list1.sort()
# print(list1)
# ['800820', 'Shanxi', 'Yulin', 'test']
# list2=
# list2.sort()
# print(list2)整形int
#

# sorted()函数:
list1 = ["Shanxi","Yulin","test","800820"]
# list2 = sorted(list1,reverse=True)
# reverse=True=降序
# print(list2)降序
# ['test', 'Yulin', 'Shanxi', '800820']
# list3 = sorted(list1,reverse=False)
# print(list3)升序排列
# ['800820', 'Shanxi', 'Yulin', 'test']

# reveres(函数,翻转列表)
# list1 =
# list1.reverse()
# print(list1)
# ['xian', 'nihao', 7, 9, 2, 6, 3, 1]
# )]
# ['xian', 'nihao', 7, 9, 2, 6, 3, 1]

# pop函数删除列表中的元素
list1 = ["Shanxi","Yulin","test","800820"]
# print(list1)
# Shanxi,删除的值可以被打印出来
# print((type(list1)))<class 'list'>
# print(list1)删除list1所有数据,并打印出来
# ['Shanxi', 'Yulin', 'test', '800820']

# python中的元组:
# tuple1 = ("nihao","xian",888,["nizuijhuanhaoma"],999)
# print(tuple1)
# ('nihao', 'xian', 888, ['nizuijhuanhaoma'], 999)
# print(type(tuple1))<class 'tuple'>类型为元组

# tuple1 = ("nihao",)
# print(tuple1)
# ('nihao',)
# 索引与切片
# tuple1 = ("nihao","xian",888,["nizuijhuanhaoma"],999)
# print(tuple1)#888索引
# print(type(tuple1)<class 'int'>
# print(tuple1)nihao
# print(type(tuple1))<class 'str'>
# print(tuple1)['nizuijhuanhaoma']
# print(type(tuple1))<class 'list'>
# tuple1 = "changan"元素的值不能直接更改

# tuple1 = ("nihao","xian",888,["nizuijhuanhaoma"],999)
# list1 = list(tuple1)
# print(type(list1))<class 'list'>
# tuple2 = tuple(list1)
# print(type(tuple2))<class 'tuple'>
# print(tuple1)
# ('nihao', 'xian', 888, ['nizuijhuanhaoma'], 999)

# 元组的迭代,需要用for循环对元组进行迭代取值:
# tuple1 = ("nihao","xian",888,["nizuijhuanhaoma"],999)
# for i in tuple1:
#   print(i)999

# python中的字典
# 字典(dict),表达式为dict1={},也叫花括号
# 字典是无序的   key,valuekey是唯一,value可以不唯一
# dict1={"shanxi":"jingbian","yuan":24}
# print(dict1){'shanxi': 'jingbian', 'yuan': 24}
# print(type(dict1))<class 'dict'>

# 定义字典用dict()函数来定义
# test=[("shanxi","jingbian"),("yuan",24)]
# dict1 = dict(test)
# # print(dict1)
# print(type(dict1))<class 'dict'>
# dict1={"name":"yuanyuan","age":24}
# dict1["sex"] = "woman"
# print(dict1)
# {'name': 'yuanyuan', 'age': 24, 'sex': 'woman'}
# dict1={"name":"yuanyuan","age":24}
# for i in dict1:
#   print(i)
# 加入修饰函数keys, 取得是键
# dict1={"name":"yuanyuan","age":24}
# for i in dict1.keys():
#   print(i)
# 加入修饰函数value, 取得是值
# for i in dict1.values():
#   print(i)
# 取具体的值
# dict1={"name":"yuanyuan","age":24}
# print(dict1["name"])yuanyuan
# print(dict1["age"])24

# setdefault函数,给字典添加键值对
# dict1={"name":"yuanyuan","age":24}
# dict1.setdefault("sex","woman")
# print(dict1)
# {'name': 'yuanyuan', 'age': 24, 'sex': 'woman'}

# 删除字典中的元素,根据键来删除值
dict1={"name":"yuanyuan","age":24}
# del dict1["name"]
# print(dict1){'age': 24}

# 删除整个字典
# dict1={"name":"yuanyuan","age":24}
# del dict1
# print(dict1)
# name 'dict1' is not defined字典全部被删除

# pop函数
# dict1={"name":"yuanyuan","age":24}
# dict1.pop("name")
# print(dict1)
# {'age': 24}

# clear清空字典所有数据
# dict1={"name":"yuanyuan","age":24}
# dict1.clear()
# print(dict1){}

# 通过for循环取字典中的键与值
# dict1={"name":"yuanyuan","age":24}
# for i in dict1:
#   print(i,dict1)
# name yuanyuan
# age 24

# 用修饰函数items取字典中的键与值
# dict1={"name":"yuanyuan","age":24}
# for k,v in dict1.items():
#   print(k,v)

#通过get函数,取键的值
# dict1={"name":"yuanyuan","age":24}
# print(dict1.get("name"))yuanyuan
# age1 = dict1.get("age")
# print(age1)24

# fromkeys函数,定义一个初始化的字典
# dict1={}.fromkeys(["name","age"])
# dict1["name"] = "lulu"
# print(dict1)
# # {'name': 'lulu', 'age': None}
# dict1["age"] = 26
# print(dict1)
# # {'name': 'lulu', 'age': 26}
# print(dict1)
# {'name': 'lulu', 'age': 26}

# __contains__函数,表示私有的函数,判断是否有某个键,
# 返回布尔值
# dict1={"name":"yuanyuan","age":24}
# print(dict1.__contains__("name"))True
# print(dict1.__contains__("name1"))False

# if dict1.__contains__("name1") is True:
#   print("判定成功")
# else:
#   dict1.setdefault("name1")
#   print(dict1)
#   dict1["name1"] = "lulu"
#   print(dict1)
#   {'name': 'yuanyuan', 'age': 24, 'name1': 'lulu'}

# popitem()函数,删除字典中的键值,同时删除键与值
# dict1={"name":"yuanyuan","age":24}
# pop =dict1.popitem()
# print(dict1){'name': 'yuanyuan'}
# print(type(pop))<class 'tuple'>

# update()函数,利用一个字典更新另外一个字典
# dict1={"name":"yuanyuan","age":24}
# dict2 = {"sex":"man"}
# dict1.update(dict2)
# print(dict1)
# {'name': 'yuanyuan', 'age': 24, 'sex': 'man'}

# python中的集合

name="yuanyuan"
# set1 =set(name)
# set1.clear()
# print(set1)set()
# set1.pop()
# print(set1)
# {'y', 'n', 'a'}
# set1.remove("y")
# print(set1)
# {'u', 'n', 'a'}
# set1.add("yang")
# print(set1)
# {'yang', 'a', 'u', 'n', 'y'}
# 深浅拷贝
# 浅拷贝copy()
# 深拷贝deepcopy()
# name="lulu66"
# set1=frozenset(name)
# print(set1)
# set2=set1.copy()
# print(set2)
# frozenset({'l', 'u', '6'})
# frozenset({'l', 'u', '6'})

list1 = [1,8,8,9,2,0,2,9,7,3,7]
set1 =set(list1)
print(set1)
# # {0, 1, 2, 3, 7, 8, 9}
list2 =list(set1)
print(list2)
# {0, 1, 2, 3, 7, 8, 9}
#

# list2=[]
# for i in set(list1):
#   list2.append(i)
#   print(list2)
#
页: [1]
查看完整版本: 4.14作业