《python 核心编程第二版第7章》习题

来源:互联网 发布:广州站西钟表城淘宝 编辑:程序博客网 时间:2024/05/22 04:52

7–1.   字典方法。哪个字典方法可以用来把两个字典合并到一起?

dict.update(dict2)   将字典 dict2 的键-值对添加到字典 dict

例:

# -*- coding:utf-8 -*-dict1 = dict([['x',1],['y',2]])dict2 = dict([['z',3],['o',4]])dict1.update(dict2)print dict1

7–2.   字典的键。我们知道字典的值可以是任意的 Python 对象,那字典的键又如何呢?请试 着将除数字和字符串以外的其他不同类型的对象作为字典的键,看一看,哪些类型可以,哪些不行? 对那些不能作字典的键的对象类型,你认为是什么原因呢?

键:必须是可哈希的。所有不可变的类型都是可哈希的,因此它们都可以做为字典的键。

较为特殊的情况:

同时,也有一些可变对象(很少)是可哈希的,它们可以做字典的键,但很少见。举一个例子, 一个实现了__hash__() 特殊方法的类。因为__hash__()方法返回一个整数,所以仍然是用不可变 的值(做字典的键)。  

为什么键必须是可哈希的?

解释器调用哈希函数,根据字典中键的值来计算存储你的数据的位 置。如果键是可变对象,它的值可改变。如果键发生变化,哈希函数会映射到不同的地址来存储数 据。如果这样的情况发生,哈希函数就不可能可靠地存储或获取相关的数据。选择可哈希的键的原 因就是因为它们的值不能改变。


数字和字符串可以被用做字典的键,用元组做有效的键,必须要加限制:元 组中只包括像数字和字符串这样的不可变参数,才可以作为字典中有效的键。  

7–3.   字典和列表的方法。

(a) 创建一个字典,并把这个字典中的键按照字母顺序显示出来。

# -*- coding:utf-8 -*-dictTest = {'x':1,'y':2,'z':3,'o':4,'p':5,'q':6}for key in sorted(dictTest):print key

(b) 现在根据已按照字母顺序排序好的键,显示出这个字典中的键和值。

# -*- coding:utf-8 -*-dictTest = {'x':1,'y':2,'z':3,'o':4,'p':5,'q':6}for key in sorted(dictTest):print key,dictTest[key]


(c)同(b),但这次是根据已按照字母顺序排序好的字典的值,显示出这个字典中的键和值。(注 意:对字典和哈希表来说,这样做一般没有什么实际意义,因为大多数访问和排序(如果需要)都是 基于字典的键,这里只把它作为一个练习。)

+ +

没弄出来,只把值排序后输出:

# -*- coding:utf-8 -*-dictTest = {'x':1,'y':2,'z':3,'o':4,'p':5,'q':6}dict_=sorted(dictTest.values())print dict_

7-4.   建立字典。给定两个长度相同的列表,比如说,列表[1, 2, 3,...]和['abc', 'def', 'ghi',...],用这两个列表里的所有数据组成一个字典,像这样:{1:'abc', 2: 'def', 3: 'ghi',...} 

# -*- coding:utf-8 -*-list1 = [1,2,3,4,5]list2 = ['a','b','c','d','e']dict_ = dict(zip(list1,list2))print dict_

7–5. userpw2.py. 下面的问题和例题 7.1 中管理名字-密码的键值对数据的程序有关。

(a)修改那个脚本,使它能记录用户上次的登录日期和时间(用 time 模块),并与用户密码一起 保存起来。程序的界面有要求用户输入用户名和密码的提示。无论户名是否成功登录,都应有提示, 在户名成功登录后,应更新相应用户的上次登录时间戳。如果本次登录与上次登录在时间上相差不 超过 4 个小时,则通知该用户: “You already logged in at: <last_ login_timestamp>.”

# -*- coding:utf-8 -*-db = {}timeUserRegister = {}import timetimeRegister = 0def newUser():while True:userName = raw_input("please input an userName:  ")if db.has_key(userName):print "the user is already exit,please try another one: "breakelse:userPassword = raw_input("please input the userPassword: ")db[userName] = userPasswordtimeRegister= time.asctime()timeUserRegister[userName] = timeRegisterbreakdef oldUser():while True:userName = raw_input("please input a userName: ")if db.has_key(userName):userPassword = raw_input("please input a userPassword: ")if userPassword == db[userName]:print "Welcome " + userName +'  the register time is:  '+timeUserRegister[userName]breakelse:print "the password is not correct please try again"else:print "the datebase has't this user"breakif __name__ == '__main__':while True:choice = raw_input("please enter an letter: ")if choice == 'n':newUser()continueelif choice =='o':oldUser()continueelse:print "the choice is unknow "break

(b) 添加一个“管理”菜单,其中有以下两项:(1)删除一个用户 (2)显示系统中所有用户的名 字和他们的密码的清单。

# -*- coding:utf-8 -*-db = {}timeUserRegister = {}import timetimeRegister = 0def newUser():while True:userName = raw_input("please input an userName:  ")if db.has_key(userName):print "the user is already exit,please try another one: "breakelse:userPassword = raw_input("please input the userPassword: ")db[userName] = userPasswordtimeRegister= time.asctime()timeUserRegister[userName] = timeRegisterbreakdef oldUser():while True:userName = raw_input("please input a userName: ")if db.has_key(userName):userPassword = raw_input("please input a userPassword: ")if userPassword == db[userName]:print "Welcome " + userName +'  the register time is:  '+timeUserRegister[userName]breakelse:print "the password is not correct please try again"else:print "the datebase has't this user"breakif __name__ == '__main__':while True:choice = raw_input("please enter an letter: ")if choice == 'n':newUser()continueelif choice =='o':oldUser()continueelif choice == 'd':userName = raw_input("please input the key you want to delet: ")db.pop(userName)continueelif choice == 's':print dbcontinueelse:print "the input is un correct :"break


(c) 口令目前没有加密。请添加一段对口令加密的代码(请参考crypt, rotor, 或其它加密模块)

= =不好意思这题没打

(d) 为程序添加图形界面,例如,用 Tkinter 写。

= = 这题也没打 学到gui 的时候在来补

(e) 要求用户名不区分大小写。

# -*- coding:utf-8 -*-db = {}timeUserRegister = {}import timetimeRegister = 0def newUser():while True:userName = raw_input("please input an userName:  ")userNameLower = userName.lower()if db.has_key(userNameLower):print "the user is already exit,please try another one: "breakelse:userPassword = raw_input("please input the userPassword: ")db[userNameLower] = userPasswordtimeRegister= time.asctime()timeUserRegister[userNameLower] = timeRegisterbreakdef oldUser():while True:userName = raw_input("please input a userName: ")userNameLower = userName.lower()if db.has_key(userNameLower):userPassword = raw_input("please input a userPassword: ")if userPassword == db[userNameLower]:print "Welcome " + userNameLower +'  the register time is:  '+timeUserRegister[userNameLower]breakelse:print "the password is not correct please try again"else:print "the datebase has't this user"breakif __name__ == '__main__':while True:choice = raw_input("please enter an letter: ")if choice == 'n':newUser()continueelif choice =='o':oldUser()continueelif choice == 'd':userName = raw_input("please input the key you want to delet: ")db.pop(userName)continueelif choice == 's':print dbcontinueelse:print "the input is un correct :"break

(f) 加强对用户名的限制,不允许符号和空白符。

# -*- coding:utf-8 -*-db = {}timeUserRegister = {}import timetimeRegister = 0def newUser():while True:userName = raw_input("please input an userName:  ")userNameLower = userName.lower()if not userNameLower.isalnum():print "please input valid try again:"continueif db.has_key(userNameLower):print "the user is already exit,please try another one: "breakelse:userPassword = raw_input("please input the userPassword: ")db[userNameLower] = userPasswordtimeRegister= time.asctime()timeUserRegister[userNameLower] = timeRegisterbreakdef oldUser():while True:userName = raw_input("please input a userName: ")userNameLower = userName.lower()if db.has_key(userNameLower):userPassword = raw_input("please input a userPassword: ")if userPassword == db[userNameLower]:print "Welcome " + userNameLower +'  the register time is:  '+timeUserRegister[userNameLower]breakelse:print "the password is not correct please try again"else:print "the datebase has't this user"breakif __name__ == '__main__':while True:choice = raw_input("please enter an letter: ")if choice == 'n':newUser()continueelif choice =='o':oldUser()continueelif choice == 'd':userName = raw_input("please input the key you want to delet: ")db.pop(userName)continueelif choice == 's':print dbcontinueelse:print "the input is un correct :"break


(g)合并“新用户”和“老用户”两个选项。如果一个新用户试图用一个不存在的用户名登录, 询问该用户是否是新用户,如果回答是肯定的,就创建该帐户。否则,按照老用户的方式登录。

# -*- coding:utf-8 -*-db = {}timeUserRegister = {}import timetimeRegister = 0def newUser():while True:userName = raw_input("please input an userName:  ")userNameLower = userName.lower()if not userNameLower.isalnum():print "please input valid try again:"continueif db.has_key(userNameLower):print "the user is already exit,please try another one: "breakelse:userPassword = raw_input("please input the userPassword: ")db[userNameLower] = userPasswordtimeRegister= time.asctime()timeUserRegister[userNameLower] = timeRegisterbreakdef oldUser():while True:userName = raw_input("please input a userName: ")userNameLower = userName.lower()if db.has_key(userNameLower):userPassword = raw_input("please input a userPassword: ")if userPassword == db[userNameLower]:print "Welcome " + userNameLower +'  the register time is:  '+timeUserRegister[userNameLower]breakelse:print "the password is not correct please try again"else:print "if you are a new user ?"answer = raw_input("please input your answer: ")if answer == 'y':userPassword = raw_input("please input your password: ")db[userNameLower] = userPasswordtimeUserRegister[userNameLower] = time.asctime()else:breakif __name__ == '__main__':while True:choice = raw_input("please enter an letter: ")if choice == 'n':newUser()continueelif choice =='o':oldUser()continueelif choice == 'd':userName = raw_input("please input the key you want to delet: ")db.pop(userName)continueelif choice == 's':print dbcontinueelse:print "the input is un correct :"break







原创粉丝点击