7.
# -*- 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.
# -*- 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.
# -*- 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. 可哈希的可以作为字典合法的键,如字符串、数字。
12.①集合是指具有某种特定性质的具体的或抽象的对象汇总成的集体,这些对象称为该集合的元素
②Python中的集合是一组无序排列的,可哈希的值
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