当前位置:   article > 正文

Python学习(八):返回值&变量的作用域&参数的传递&浅拷贝和深拷贝&类对象和方法_python return 字典是浅拷贝吗

python return 字典是浅拷贝吗

Python学习分享:

今日学习:返回值&变量的作用域&参数的传递&浅拷贝和深拷贝&类对象和方法
  • 1

返回值
return 返回值要点:
1.如果函数体中包含return语句,则结束函数执行并返回值
2.如果函数体中不包含return语句,则返回none值
3.要返回多个返回值,使用列表,元组,字典,集合将多个值“存起来”即可。

# 定义一个返回两个数平均值的函数:
def my_avg(a,b):
    return (a+b)/2

print(my_avg(2, 3))
  • 1
  • 2
  • 3
  • 4
  • 5

函数也是对象,内存底层分析
Python中,“一切都是对象”,实际上,执行def函数后,系统就创建了相应的函数对象

变量的作用域(全局变量和局部变量)

变量起作用的范围称为变量的作用域,不同作用域内同名变量互不影响。变量分为:全局变量,局部变量
全局变量: 1.在函数和类定义之外声明的变量。作用域为定义的模块,从定义位置开始到模块结束
2.全局变量降低了函数的通用性和可读性,应尽量避免全局变量的使用
3.全局变量一般做常量使用
4.函数内要改变全局变量的值,使用global声明一下
局部变量:1.在函数体中(包含形参)声明的变量
2.局部变量的引用比全局变量快,优先考虑使用
3.如果局部变量和全局变量同名,则在函数内隐藏全局变量,只使用同名的局部变量

a = 100
def cs():
    b = 4
    global a
    print(a)
    a = 200
    print(a)
    print(locals()) # 打印输出的局部变量
    print(globals()) # 打印输出的全局变量
cs()
打印结果为:
100
200
print(a)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

局部变量和全局变量效率测试:
局部变量的查询和访问速度比全局变量快,优先考虑使用,尤其是在循环的时候
在特别强调效率的地方或者循环次数较多的地方,可以通过将全局变量转为局部变量提高运行速度。

import math
import time
def text():
    start = time.time()
    for i in range(10000000):
        math.sqrt(30)
    end=time.time()
    print("{0}".format(end-start))

def text02():
    b = math.sqrt
    start = time.time()
    for i in range(10000000):
        b(30)
    end=time.time()
    print("{0}".format(end-start))
结果为:
2.2087323665618896
1.957878589630127
text()
text02()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

参数的传递
函数声明时的形参,使用时,等同于函数体内的局部变量。由于Python中一切皆为对象。因此,参数传递时直接传递对象的地址,但具体使用分两种类型:
1.传递不可变对象的引用(起到其他语言值传递的效果) 数字,字符串,元组,function等
2.传递可变对象的引用(起到其他语言引用传递的效果) 字典,列表,集合,自定义的对象等

传递可变对象的引用:
可以改变,但对象不变
b = [10, 20]
def f(m):
    print(m,id(m))
    m.append(20)

f(b)
print(id(b))
print(b)


传递不可变对象的引用:
a = 100
def f(m):
    print(m,id(m))
    # 这里会创建一个新对象
    m=m+20
    print(m,id(m))

f(a)
print(id(a),a)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

深拷贝和浅拷贝 内置函数:copy(浅拷贝),deepcpy(深拷贝)
浅拷贝:不拷贝子对象的内容,只是拷贝子对象的引用
深拷贝:会连子对象的内存也全部拷贝一份,对子对象的修改不会影响源对象

import copy
a = [10, 20, [5, 6]]
b = copy.copy(a)
print("b:", b)
b.append(30)
b[2].append(7)
print("浅拷贝测试结果")
print("a:", a)
print("b:", b)

a = [10, 20, [5, 6]]
b = copy.deepcopy(a)
print("b:", b)
b.append(30)
b[2].append(7)
print("深拷贝测试结果")
print("a:", a)
print("b:", b)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

传递不可变对象是浅拷贝

# 传递不可变对象是浅拷贝
# 传递参数是不可变对象,实际传递的还是对象的引用。但在“写操作”时,会创建一个新的对象拷贝。这个拷贝使用的是“浅拷贝”,不是“深拷贝”
a = (10, 20, [5, 6])
print(a)
def test01(m):
    print("m:",id(m))
    m[2][0] = 20
    print(m)
    print(id(m))

test01(a)
print(a)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

类对象:

class Student:
    name =str
    sex = str
    age = int

    def say(self):
        '''self 这个参数是当前这个对象
                这个参数不需要传参
        '''
        print("hello")


s = Student()
s2 = Student()
print(s)
s.say()
s.name="小李"
# s有几个name属性?两个:name类属性,name实例属性
del s.name # 删除了实例name属性
s.birth = '2020-1-1' # python是弱类型语言
print(s.name)
print(s.birth)
print(s2.name)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

类中的方法:

class Student:
# 私有化属性    加一个_
    _name: str
    _age : int
    # 提供get/set方法
    def get_name(self):
        return self._name
    def set_name(self,name:str):
         self._name=name
# 初始化方法
    def __init__(self,name:str="",age:int=0):
        self._name = name
        self._age = age
    # tostring方法
    def __str__(self):
        return f"名字{self._name},年龄{self._age}"
    #静态方法
    @staticmethod
    def say():
        print("h")
    # 不重要的方法写在这
    @classmethod
    def test(cls):
        cls._name="xx"

s = Student()
s.test()
print(s.get_name())
s.say()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

简单方法测试(控制台版电影管理系统):

movies = [{"id": "1292052", "name": "肖申克的救赎", "rating": 9.7, "inq": "希望让人自由。"},
          {"id": "1291546", "name": "霸王别姬", "rating": 9.6, "inq": "风华绝代。"},
          {"id": "1292720", "name": "阿甘正传", "rating": 9.5, "inq": "一部美国近现代史。"},
          {"id": "1295644", "name": "这个杀手不太冷", "rating": 9.4, "inq": "怪蜀黍和小萝莉不得不说的故事。"},
          {"id": "1292722", "name": "泰坦尼克号", "rating": 9.4, "inq": "失去的才是永恒的。"},
          {"id": "1292063", "name": "美丽人生", "rating": 9.6, "inq": "最美的谎言。"},
          {"id": "1291561", "name": "千与千寻", "rating": 9.4, "inq": "最好的宫崎骏,最好的久石让。"},
          {"id": "1295124", "name": "辛德勒的名单", "rating": 9.5, "inq": "拯救一个人,就是拯救整个世界。"},
          {"id": "3541415", "name": "盗梦空间", "rating": 9.3, "inq": "诺兰给了我们一场无法盗取的梦。"},
          {"id": "3011091", "name": "忠犬八公的故事", "rating": 9.4, "inq": "永远都不能忘记你所爱的人。"},
          {"id": "1889243", "name": "星际穿越", "rating": 9.3, "inq": "爱是一种力量,让我们超越时空感知它的存在。"},
          {"id": "1292064", "name": "楚门的世界", "rating": 9.3, "inq": "如果再也不能见到你,祝你早安,午安,晚安。"},
          {"id": "1292001", "name": "海上钢琴师", "rating": 9.3, "inq": "每个人都要走一条自己坚定了的路,就算是粉身碎骨。"},
          {"id": "3793023", "name": "三傻大闹宝莱坞", "rating": 9.2, "inq": "英俊版憨豆,高情商版谢耳朵。"},
          {"id": "2131459", "name": "机器人总动员", "rating": 9.3, "inq": "小瓦力,大人生。"},
          {"id": "1291549", "name": "放牛班的春天", "rating": 9.3, "inq": "天籁一般的童声,是最接近上帝的存在。"},
          {"id": "1307914", "name": "无间道", "rating": 9.3, "inq": "香港电影史上永不过时的杰作。"},
          {"id": "25662329", "name": "疯狂动物城", "rating": 9.2, "inq": "迪士尼给我们营造的乌托邦就是这样,永远善良勇敢,永远出乎意料。"},
          {"id": "1292213", "name": "大话西游之大圣娶亲", "rating": 9.2, "inq": "一生所爱。"},
          {"id": "5912992", "name": "熔炉", "rating": 9.3, "inq": "我们一路奋战不是为了改变世界,而是为了不让世界改变我们。"},
          {"id": "1291841", "name": "教父", "rating": 9.3, "inq": "千万不要记恨你的对手,这样会让你失去理智。"},
          {"id": "1849031", "name": "当幸福来敲门", "rating": 9.1, "inq": "平民励志片。"},
          {"id": "1291560", "name": "龙猫", "rating": 9.2, "inq": "人人心中都有个龙猫,童年就永远不会消失。"},
          {"id": "1296141", "name": "控方证人", "rating": 9.6, "inq": "比利·怀德满分作品。"},
          {"id": "3319755", "name": "怦然心动", "rating": 9.1, "inq": "真正的幸福是来自内心深处。"},
          {"id": "6786002", "name": "触不可及", "rating": 9.3, "inq": "满满温情的高雅喜剧。"},
          {"id": "1293172", "name": "末代皇帝", "rating": 9.3, "inq": "“不要跟我比惨,我比你更惨”再适合这部电影不过了。"},
          {"id": "1851857", "name": "蝙蝠侠:黑暗骑士", "rating": 9.2, "inq": "无尽的黑暗。"},
          {"id": "20495023", "name": "寻梦环游记", "rating": 9.1, "inq": "死亡不是真的逝去,遗忘才是永恒的消亡。"},
          {"id": "1292365", "name": "活着", "rating": 9.3, "inq": "张艺谋最好的电影。"},
          {"id": "1291552", "name": "指环王3:王者无敌", "rating": 9.3, "inq": "史诗的终章。"},
          {"id": "1295038", "name": "哈利·波特与魔法石", "rating": 9.1, "inq": "童话世界的开端。"},
          {"id": "1300267", "name": "乱世佳人", "rating": 9.3, "inq": "Tomorrow is another day."},
          {"id": "30170448", "name": "何以为家", "rating": 9.1, "inq": "凝视卑弱生命,用电影改变命运。"},
          {"id": "21937452", "name": "素媛", "rating": 9.3, "inq": "受过伤害的人总是笑得最开心,因为他们不愿意让身边的人承受一样的痛苦。"},
          {"id": "2129039", "name": "飞屋环游记", "rating": 9.1, "inq": "最后那些最无聊的事情,才是最值得怀念的。"},
          {"id": "26387939", "name": "摔跤吧!爸爸", "rating": 9.0, "inq": "你不是在为你一个人战斗,你要让千千万万的女性看到女生并不是只能相夫教子。"},
          {"id": "1293182", "name": "十二怒汉", "rating": 9.4, "inq": "1957年的理想主义。"},
          {"id": "1308807", "name": "哈尔的移动城堡", "rating": 9.1, "inq": "带着心爱的人在天空飞翔。"},
          {"id": "1929463", "name": "少年派的奇幻漂流", "rating": 9.1, "inq": "瑰丽壮观、无人能及的冒险之旅。"},
          {"id": "26752088", "name": "我不是药神", "rating": 9.0, "inq": "对我们国家而言,这样的电影多一部是一部。"},
          {"id": "1291858", "name": "鬼子来了", "rating": 9.3, "inq": "对敌人的仁慈,就是对自己残忍。"},
          {"id": "1299398", "name": "大话西游之月光宝盒", "rating": 9.0, "inq": "旷古烁今。"},
          {"id": "1291583", "name": "天空之城", "rating": 9.1, "inq": "对天空的追逐,永不停止。"},
          {"id": "1291828", "name": "天堂电影院", "rating": 9.2, "inq": "那些吻戏,那些青春,都在影院的黑暗里被泪水冲刷得无比清晰。"},
          {"id": "1305487", "name": "猫鼠游戏", "rating": 9.1, "inq": "骗子大师和执著警探的你追我跑故事。"},
          {"id": "1298624", "name": "闻香识女人", "rating": 9.1, "inq": "史上最美的探戈。"},
          {"id": "1291572", "name": "指环王2:双塔奇兵", "rating": 9.2, "inq": "承前启后的史诗篇章。"},
          {"id": "1293839", "name": "罗马假日", "rating": 9.1, "inq": "爱情哪怕只有一天。"},
          {"id": "1296736", "name": "钢琴家", "rating": 9.2, "inq": "音乐能化解仇恨。"},
          {"id": "1291571", "name": "指环王1:护戒使者", "rating": 9.1, "inq": "传说的开始。"},
          {"id": "21937445", "name": "辩护人", "rating": 9.2, "inq": "电影的现实意义大过电影本身。"},
          {"id": "3742360", "name": "让子弹飞", "rating": 8.9, "inq": "你给我翻译翻译,神马叫做TMD的惊喜。"},
          {"id": "1418019", "name": "大闹天宫", "rating": 9.4, "inq": "经典之作,历久弥新。"},
          {"id": "1299131", "name": "教父2", "rating": 9.2, "inq": "优雅的孤独。"},
          {"id": "1301753", "name": "狮子王", "rating": 9.1, "inq": "动物版《哈姆雷特》。"},
          {"id": "1291548", "name": "死亡诗社", "rating": 9.1, "inq": "当一个死水般的体制内出现一个活跃的变数时,所有的腐臭都站在了光明的对面。"},
          {"id": "1291843", "name": "黑客帝国", "rating": 9.1, "inq": "视觉革命。"},
          {"id": "1292000", "name": "搏击俱乐部", "rating": 9.0, "inq": "邪恶与平庸蛰伏于同一个母体,在特定的时间互相对峙。"},
          {"id": "25958717", "name": "海蒂和爷爷", "rating": 9.2, "inq": "如果生活中有什么使你感到快乐,那就去做吧!不要管别人说什么。"},
          {"id": "27060077", "name": "绿皮书", "rating": 8.9, "inq": "去除成见,需要勇气。"},
          {"id": "1291818", "name": "饮食男女", "rating": 9.1, "inq": "人生不能像做菜,把所有的料都准备好了才下锅。"},
          {"id": "1306029", "name": "美丽心灵", "rating": 9.0, "inq": "爱是一切逻辑和原由。"},
          {"id": "1900841", "name": "窃听风暴", "rating": 9.2, "inq": "别样人生。"},
          {"id": "1485260", "name": "本杰明·巴顿奇事", "rating": 8.9, "inq": "在时间之河里感受溺水之苦。"},
          {"id": "1293350", "name": "两杆大烟枪", "rating": 9.1, "inq": "4个臭皮匠顶个诸葛亮,盖·里奇果然不是盖的。"},
          {"id": "1292220", "name": "情书", "rating": 8.9, "inq": "暗恋的极致。"},
          {"id": "3008247", "name": "穿条纹睡衣的男孩", "rating": 9.1, "inq": "尽管有些不切实际的幻想,这部电影依旧是一部感人肺腑的佳作。"},
          {"id": "1292402", "name": "西西里的美丽传说", "rating": 8.9, "inq": "美丽无罪。"},
          {"id": "26580232", "name": "看不见的客人", "rating": 8.8, "inq": "你以为你以为的就是你以为的。"},
          {"id": "1292224", "name": "飞越疯人院", "rating": 9.1, "inq": "自由万岁。"},
          {"id": "1292849", "name": "拯救大兵瑞恩", "rating": 9.0, "inq": "美利坚精神输出大片No1."},
          {"id": "1294408", "name": "音乐之声", "rating": 9.1, "inq": "用音乐化解仇恨,让歌声串起美好。"},
          {"id": "1303021", "name": "小鞋子", "rating": 9.2, "inq": "奔跑的孩子是天使。"},
          {"id": "1652587", "name": "阿凡达", "rating": 8.8, "inq": "绝对意义上的美轮美奂。"},
          {"id": "3442220", "name": "海豚湾", "rating": 9.3, "inq": "海豚的微笑,是世界上最高明的伪装。"},
          {"id": "1780330", "name": "致命魔术", "rating": 8.9, "inq": "孪生蝙蝠侠大战克隆金刚狼。"},
          {"id": "1293544", "name": "沉默的羔羊", "rating": 8.9, "inq": "安东尼·霍普金斯的顶级表演。"},
          {"id": "1292262", "name": "美国往事", "rating": 9.2, "inq": "往事如烟,无处祭奠。"},
          {"id": "3011235", "name": "哈利·波特与死亡圣器(下)", "rating": 8.9, "inq": "10年的完美句点。"},
          {"id": "2334904", "name": "禁闭岛", "rating": 8.8, "inq": "昔日翩翩少年,今日大腹便便。"},
          {"id": "1292343", "name": "蝴蝶效应", "rating": 8.8, "inq": "人的命运被自己瞬间的抉择改变。"},
          {"id": "11525673", "name": "布达佩斯大饭店", "rating": 8.9, "inq": "小清新的故事里注入了大历史的情怀。"},
          {"id": "1292656", "name": "心灵捕手", "rating": 8.9, "inq": "人生中应该拥有这样的一段豁然开朗。"},
          {"id": "1291832", "name": "低俗小说", "rating": 8.9, "inq": "故事的高级讲法。"},
          {"id": "1292679", "name": "春光乍泄", "rating": 9.0, "inq": "爱情纠缠,男女一致。"},
          {"id": "1292223", "name": "七宗罪", "rating": 8.8, "inq": "警察抓小偷,老鼠玩死猫。"},
          {"id": "1294371", "name": "摩登时代", "rating": 9.3, "inq": "大时代中的人生,小人物的悲喜。"},
          {"id": "1302425", "name": "喜剧之王", "rating": 8.8, "inq": "我是一个演员。"},
          {"id": "1297192", "name": "致命ID", "rating": 8.8, "inq": "最不可能的那个人永远是最可能的。"},
          {"id": "1787291", "name": "被嫌弃的松子的一生", "rating": 8.9, "inq": "以戏谑来戏谑戏谑。"},
          {"id": "1300299", "name": "杀人回忆", "rating": 8.9, "inq": "关于连环杀人悬案的集体回忆。"},
          {"id": "1298070", "name": "加勒比海盗", "rating": 8.8, "inq": "约翰尼·德普的独角戏。"},
          {"id": "1292370", "name": "剪刀手爱德华", "rating": 8.7, "inq": "浪漫忧郁的成人童话。"},
          {"id": "1865703", "name": "红辣椒", "rating": 9.0, "inq": "梦的勾结。"},
          {"id": "6985810", "name": "狩猎", "rating": 9.1, "inq": "人言可畏。"},
          {"id": "26799731", "name": "请以你的名字呼唤我", "rating": 8.9, "inq": "沉醉在电影的情感和视听氛围中无法自拔。"},
          {"id": "1294639", "name": "勇敢的心", "rating": 8.9, "inq": "史诗大片的典范。"},
          {"id": "10777687", "name": "7号房的礼物", "rating": 8.9, "inq": "《我是山姆》的《美丽人生》。"},
          {"id": "1418834", "name": "断背山", "rating": 8.8, "inq": "每个人心中都有一座断背山。"},
          {"id": "1291543", "name": "功夫", "rating": 8.7, "inq": "警恶惩奸,维护世界和平这个任务就交给你了,好吗?"},
          {"id": "5322596", "name": "超脱", "rating": 8.9, "inq": "穷尽一生,我们要学会的,不过是彼此拥抱。"},
          {"id": "1292215", "name": "天使爱美丽", "rating": 8.7, "inq": "法式小清新。"},
          {"id": "1291544", "name": "哈利·波特与阿兹卡班的囚徒", "rating": 8.8, "inq": "不一样的导演,不一样的哈利·波特。"},
          {"id": "1297630", "name": "第六感", "rating": 8.9, "inq": "深入内心的恐怖,出人意料的结局。"},
          {"id": "1297359", "name": "幽灵公主", "rating": 8.9, "inq": "人与自然的战争史诗。"},
          {"id": "1306249", "name": "唐伯虎点秋香", "rating": 8.7, "inq": "华太师是黄霑,吴镇宇四大才子之一。"},
          {"id": "1291999", "name": "重庆森林", "rating": 8.8, "inq": "寂寞没有期限。"},
          {"id": "25814705", "name": "小森林 夏秋篇", "rating": 9.0, "inq": "那些静得只能听见呼吸的日子里,你明白孤独即生活。"},
          {"id": "1291875", "name": "阳光灿烂的日子", "rating": 8.8, "inq": "一场华丽的意淫。"},
          {"id": "1296339", "name": "爱在黎明破晓前", "rating": 8.8, "inq": "缘分是个连绵词,最美不过一瞬。"},
          {"id": "1292434", "name": "一一", "rating": 9.1, "inq": "我们都曾经是一一。"},
          {"id": "3395373", "name": "蝙蝠侠:黑暗骑士崛起", "rating": 8.8, "inq": "诺兰就是保证。"},
          {"id": "1293359", "name": "菊次郎的夏天", "rating": 8.8, "inq": "从没见过那么流氓的温柔,从没见过那么温柔的流氓。"},
          {"id": "21318488", "name": "消失的爱人", "rating": 8.7, "inq": "年度最佳date movie。"},
          {"id": "2149806", "name": "入殓师", "rating": 8.9, "inq": "死可能是一道门,逝去并不是终结,而是超越,走向下一程。"},
          {"id": "1292337", "name": "无人知晓", "rating": 9.1, "inq": "我的平常生活就是他人的幸福。"},
          {"id": "11026735", "name": "超能陆战队", "rating": 8.7, "inq": "Balalaika~~~"},
          {"id": "25814707", "name": "小森林 冬春篇", "rating": 9.0, "inq": "尊敬他人,尊敬你生活的这片土地,明白孤独是人生的常态。"},
          {"id": "1296996", "name": "哈利·波特与密室", "rating": 8.8, "inq": "魔法的密室之门已打开..."},
          {"id": "1300992", "name": "完美的世界", "rating": 9.1, "inq": "坏人的好总是比好人的好来得更感人。"},
          {"id": "1297447", "name": "倩女幽魂", "rating": 8.7, "inq": "两张绝世的脸。"},
          {"id": "1297052", "name": "侧耳倾听", "rating": 8.9, "inq": "少女情怀总是诗。"},
          {"id": "1291990", "name": "爱在日落黄昏时", "rating": 8.9, "inq": "九年后的重逢是世俗和责任的交叠,没了悸动和青涩,沧桑而温暖。"},
          {"id": "4202302", "name": "借东西的小人阿莉埃蒂", "rating": 8.9, "inq": "曾经的那段美好会沉淀为一辈子的记忆。"},
          {"id": "1305164", "name": "甜蜜蜜", "rating": 8.9, "inq": "相逢只要一瞬间,等待却像是一辈子。"},
          {"id": "5989818", "name": "萤火之森", "rating": 8.9, "inq": "触不到的恋人。"},
          {"id": "2353023", "name": "驯龙高手", "rating": 8.7, "inq": "和谐的生活离不开摸头与被摸头。"},
          {"id": "1292274", "name": "幸福终点站", "rating": 8.8, "inq": "有时候幸福需要等一等。"},
          {"id": "3072124", "name": "玛丽和马克思", "rating": 8.9, "inq": "你是我最好的朋友,你是我唯一的朋友 。"},
          {"id": "10577869", "name": "时空恋旅人", "rating": 8.8, "inq": "把每天当作最后一天般珍惜度过,积极拥抱生活,就是幸福。"},
          {"id": "1291545", "name": "大鱼", "rating": 8.8, "inq": "抱着梦想而活着的人是幸福的,怀抱梦想而死去的人是不朽的。"},
          {"id": "4268598", "name": "告白", "rating": 8.8, "inq": "没有一人完全善,也没有一人完全恶。"},
          {"id": "4917726", "name": "阳光姐妹淘", "rating": 8.8, "inq": "再多各自牛逼的时光,也比不上一起傻逼的岁月。"},
          {"id": "1291579", "name": "怪兽电力公司", "rating": 8.7, "inq": "不要给它起名字,起了名字就有感情了。"},
          {"id": "1316510", "name": "射雕英雄传之东成西就", "rating": 8.7, "inq": "百看不厌。"},
          {"id": "3287562", "name": "神偷奶爸", "rating": 8.6, "inq": "Mr. I Don't Care其实也有Care的时候。"},
          {"id": "1418200", "name": "傲慢与偏见", "rating": 8.7, "inq": "爱是摈弃傲慢与偏见之后的曙光。"},
          {"id": "1294240", "name": "教父3", "rating": 9.0, "inq": "任何信念的力量,都无法改变命运。"},
          {"id": "21360417", "name": "恐怖直播", "rating": 8.7, "inq": "恐怖分子的“秋菊打官司”。"},
          {"id": "1858711", "name": "玩具总动员3", "rating": 8.9, "inq": "跨度十五年的欢乐与泪水。"},
          {"id": "25986180", "name": "釜山行", "rating": 8.6, "inq": "揭露人性的丧尸题材力作。"},
          {"id": "26628357", "name": "一个叫欧维的男人决定去死", "rating": 8.9, "inq": "惠及一生的美丽。"},
          {"id": "1307315", "name": "哪吒闹海", "rating": 9.1, "inq": "想你时你在闹海。"},
          {"id": "26325320", "name": "血战钢锯岭", "rating": 8.7, "inq": "优秀的战争片不会美化战场,不会粉饰死亡,不会矮化敌人,不会无视常识,最重要的,不会宣扬战争。"},
          {"id": "6307447", "name": "被解救的姜戈", "rating": 8.8, "inq": "热血沸腾,那个低俗、性感的无耻混蛋又来了。"},
          {"id": "1395091", "name": "未麻的部屋", "rating": 9.0, "inq": "好的剧本是,就算你猜到了结局也猜不到全部。"},
          {"id": "4920389", "name": "头号玩家", "rating": 8.7, "inq": "写给影迷,动漫迷和游戏迷的一封情书。"},
          {"id": "1295399", "name": "七武士", "rating": 9.3, "inq": "时代悲歌。"},
          {"id": "1306861", "name": "我是山姆", "rating": 9.0, "inq": "爱并不需要智商 。"},
          {"id": "1303037", "name": "喜宴", "rating": 8.9, "inq": "中国家庭的喜怒哀乐忍。"}]

def list():
     print("*"*40)
     print("请输入您的选择")
     print("1.增加 2.删除 3.查看 4.修改 5.退出")

def query():
    print("请输入选择:1.查看top10 2.查看top-10 3.区间查询")
    i = int(input("请输入"))
    # 排序
    movies.sort(key=lambda x: x["rating"], reverse=True)
    ms:list()
    if i == 1:
        ms = movies[:10]
    if i == 2:
        ms = movies[-1:-11:-1]
    if i == 3:
        a = int(input("上区间"))
        b = int(input("下区间"))
        ms = movies[a:b]
    for n in ms:
        print(n)


def delete():
    id = input("请输入编号:")
    for i in movies:
        if(i.get("id") == id):
            movies.remove(i)
            break;
    else:
        print("没有该电影")

def add():
    id = input("请输入编号")
    name = input("请输入名称")
    rating = float(input("请输入评分"))
    inq = input("请输入简介")
    for i in movies:
        if(i.get("id") == id):
            print("该编号已存在")
            break
    else:
        movies.append({
            "id":id,
            "name":name,
            "rating":rating,
            "inq":inq
        })

def update():
    id = input("请输入编号:")
    for i in movies:
        if (i.get("id") == id):
            movies.remove(i)
            id = input("请输入编号")
            name = input("请输入名称")
            rating = float(input("请输入评分"))
            inq = input("请输入简介")
            movies.append({
                "id": id,
                "name": name,
                "rating": rating,
                "inq": inq
            })
            break;
    else:
        print("没有该电影")


while True:
    list()
    i = int(input("请输入"))
    if i == 1:
        add()
    elif i == 2:
        delete()
    elif i == 3:
        query()
    elif i == 4:
        update()
    elif i == 5:
        break
    else:
        print("暂时没有该功能!")



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/602111
推荐阅读
相关标签
  

闽ICP备14008679号