7.python
# -*- coding:utf-8 -*- def dictconvert(d={}): if len(d) == 0: dictlen = int(raw_input(u'請輸入待轉換的字典長度'.encode('gbk'))) for i in range(dictlen): dictkeys = raw_input(u'請輸入字典的鍵'.encode('gbk')) dictvalues = raw_input(u'請輸入鍵對應的值'.encode('gbk')) d[dictkeys] = dictvalues dict_converted = {} for eachkey, eachvalue in d.items(): dict_converted[eachvalue] = eachkey return dict_converted
8.app
# -*- coding:utf-8 -*- def sortname(a_dict): for eachkey in sorted(a_dict): print u'僱員姓名:%s\t僱員編號:%s。' % (eachkey, a_dict[eachkey]) def sortnumber(a_dict): newdict = {} for eachkey, eachvalue in a_dict.items(): newdict[eachvalue] = eachkey for eachkey in sorted(newdict): print u'僱員姓名:%s\t僱員編號:%s。' % (newdict[eachkey], eachkey) def sortnone(a_dict): for eachkey, eachvalue in a_dict.items(): print u'僱員姓名:%s\t僱員編號:%s。' % (eachkey, eachvalue) def hr(): employerdata = {} amountofpeople = int(raw_input(u'請輸入僱員人數:'.encode('gbk'))) for i in range(amountofpeople): employername = raw_input(u'請輸入僱員姓名:'.encode('gbk')) employernumber = raw_input(u'請輸入僱員編號:'.encode('gbk')) employerdata[employername] = int(employernumber) while True: sortmode = raw_input(u'按僱員姓名排序輸入1,按僱員編號排序輸入2,直接顯示輸入3,退出輸入Q'.encode('gbk')) if sortmode == '1': sortname(employerdata) elif sortmode == '2': sortnumber(employerdata) elif sortmode == '3': sortnone(employerdata) else: break hr()
9.dom
# -*- coding:utf-8 -*- def tr(srcstr, dststr, string, casesensitive = True): prompt = u'錯誤!字符串中沒有須要翻譯的字符!' if casesensitive == True: if srcstr not in string: print prompt else: string = string.replace(srcstr, dststr) print string else: if srcstr.lower() not in string.lower(): print prompt else: splitstr = string.lower().split(srcstr.lower()) l = len(splitstr[0]) for i in range(len(splitstr)-1): string = string.replace(string[l:l+len(srcstr)], dststr) l = l + len(srcstr) print string
10.加密
# -*- coding:utf-8 -*- def rot13(string = None): if not string: string = raw_input(u'請輸入要加密或解密的字符串'.encode('gbk')) else: string = str(string) print u'要加密或者解密的字符串是:', string en_decrypt = [] for i in string: n = ord(i) if i.isalpha(): if n > 109 or 77 < n <= 90: en_decrypt.append(chr(n - 13)) else: en_decrypt.append(chr(n + 13)) else: en_decrypt.append(i) en_decrypt = ''.join(en_decrypt) print u'加密或者解密後的字符串爲:', en_decrypt
11. 可哈希的能夠做爲字典合法的鍵,如字符串、數字。spa
12.①集合是指具備某種特定性質的具體的或抽象的對象彙總成的集體,這些對象稱爲該集合的元素翻譯
②Python中的集合是一組無序排列的,可哈希的值code
13.對象
# -*- coding:utf-8 -*- import random def set_random_set(): N = random.randint(1,10) i = 1 random_list1 = [] while i <= N: random_list1.append(random.randint(0,9)) i += 1 return random_list1 A = set(set_random_set()) B = set(set_random_set()) print A | B print A & B
14.排序
# -*- coding:utf-8 -*- import random import sys def set_random_set(): N = random.randint(1,10) i = 1 random_list1 = [] while i <= N: random_list1.append(random.randint(0,9)) i += 1 return random_list1 A = set(set_random_set()) B = set(set_random_set()) print u'集合A是:', A print u'集合B是:', B realABunion = A | B realABintersection = A & B i = 1 ABunion = set([]) ABintersection = set([]) while i <= 3: i += 1 while True: union_element = raw_input(u'請輸入集合 A|B 的每個元素,按Q完成輸入'.encode('gbk')) if union_element.lower() == 'q': break elif union_element.isspace(): ABunion = set([]) break ABunion.add(int(union_element)) while True: intersection_element = raw_input(u'請輸入集合 A&B 的每個元素,按Q完成輸入'.encode('gbk')) if intersection_element.lower() == 'q': break elif intersection_element.isspace(): ABintersection = set([]) break ABintersection.add(int(intersection_element)) if realABunion == ABunion and realABintersection == ABintersection: print u'真厲害,徹底正確!' sys.exit() else: print u'好像有些問題,再檢查一下?' print u'你已經算錯三次了,仍是來看看正確答案吧!' print u'集合A和B的並集是:', realABunion print u'集合A和B的交集是:', realABintersection