大家好,给大家分享一下核桃编程python简单小游戏代码大全,很多人还不知道这一点。下面详细解释一下。现在让我们来看看!
一下第七章所有放上去太长了,分开一部分
7.3.a
提供两种方案,1 由于dict没有.sort方法,使用dict.keys()返回key得到一个列表,然后列表调用.sort()
2 直接用sorted()函数(参考别人所得)
#!/usr/bin/env python
dict2 = {'a': 'earth', 'port': 80}
list1 = dict2.keys()
keys = list1.sort()
print list1
print keys
7.3.b
把上边两者的答value输出,这里差别倒不大
for each_key in list1:
print each_key, dict2[each_key]
for each_key in keys:
print each_key, dict2[each_key]
7.3.c
这一次第一种方法就比较麻烦了
list1 = [[v[1],v[0]] for v in dict2.items()]
list1.sort()
lenth = len(list1)
for i in range(lenth):
print list1[i][1], list1[i][0]
第二种方法直接用lamaba
list2 = sorted(dict2.items(),key=lambda x:x[1])
7.4
比较简单zip(list1, list2)
list1 = [1, 2, 3]
list2 = ['abc', 'def', 'hij']
mydict = zip(list1, list2)
7.5单独列出
7.6单独列出
7.7
遍历一圈,把字典的key和value赋给另一个字典的value和key
试了一个value相等的,发现第二个字典只保留最后压入的
#!/usr/bin/env python
def changedict(dict1):
dict2 = {}
for item in dict1.keys():
dict2[dict1[item]] = item
return dict2
dict1 = {'a':1, 'b':2, 'c':3}
dict2 = {}.fromkeys( ('x', 'y', 'z'), -1)
print changedict(dict1)
print changedict(dict2)
7.8
把7.3的改动一下就完事了
由于雇员姓名可能重复,但是编号不会重复,这里把编号作为key
#!/usr/bin/env python
db = {}
def hire(name, num):
db[num] = name
def sortbyname():
db1 = sorted(db.items(),key=lambda x:x[1])
return db1
def sortbynum():
db1 = sorted(db.items(), key = lambda x:x[0])
return db1
def main():
while True:
name = raw_input("please input a name(end stands for back up):")
if name == 'end':
break
num = raw_input("please input a the number of the one:")
hire(name, num)
print sortbyname()
print sortbynum()
if __name__ == '__main__':
main()
7.9.a
核心dict(zip(srcstr, detstr, string)
对两个列表zip的话返回的是字典,而对两个string zip的话返回的是list所以要用dict()转换一下
#!/usr/bin/env python
def tr(srcstr, dststr, string):
codedict = dict( zip(srcstr, dststr))
enstring = ''
for each_chr in string:
if codedict.has_key(each_chr):
enstring += codedict[each_chr]
else:
enstring += each_chr
return enstring
def menu():
print'''\
(Q)uit
(E)ncrypt
'''
def main():
while True:
menu()
try:
choice = raw_input('please enter your choice:').strip()[0].lower()
except (EOFError, KeyboardInterrupt):
choice = 'q'
if choice not in 'qe':
print 'invalid option, try again'
continue
else:
if choice == 'q':break
if choice == 'e':
src = raw_input('please input your source code:')
dst = raw_input('please input your destination code:')
if len(src) != len(dst):
print "error in lenth of you source code and destination"
else:
string = raw_input('please input the string you want to transform:')
outstring = tr(src, dst, string)
print outstring
if __name__ == '__main__':
main()
7.9.b
再增加一个选项,如果选不锁定大小写的话 用.lower()处理下输入的字符串
#!/usr/bin/env python
#coding=utf-8
def tr(srcstr, dststr, string):
codedict = dict( zip(srcstr, dststr))
enstring = ''
for each_chr in string:
if codedict.has_key(each_chr):
enstring += codedict[each_chr]
else:
enstring += each_chr
return enstring
def menu():
print'''\
(Q)uit
(E)ncrypt
'''
def menu2():
print'''\
(1)大小写锁定
(2)不关注大小写
(q)uit'''
def main():
while True:
menu()
try:
choice = raw_input('please enter your choice:').strip()[0].lower()
except (EOFError, KeyboardInterrupt):
choice = 'q'
if choice not in 'qe':
print 'invalid option, try again'
continue
if choice == 'q':break
else:
while True:
menu2()
chosen = raw_input('please enter your choice:').strip()
if chosen == 'q':
break
elif chosen not in '12':
print 'invalid iption, try again'
continue
src = raw_input('please input your source code:')
dst = raw_input('please input your destination code:')
if len(src) != len(dst):
print "error in lenth of you source code and destination"
else:
string = raw_input('please input the string you want to transform:')
if chosen == '2':
src = src.lower()
dst = dst.lower()
string = string.lower()
outstring = tr(src, dst, string)
print outstring
if __name__ == '__main__':
main()
7.9c
一个函数string.replace()解决
#!/usr/bin/env python
def tr(srcstr, dststr, string):
enstring = string.replace(srcstr,dststr)
return enstring
def menu():
print'''\
(Q)uit
(E)ncrypt
'''
def main():
while True:
menu()
try:
choice = raw_input('please enter your choice:').strip()[0].lower()
except (EOFError, KeyboardInterrupt):
choice = 'q'
if choice not in 'qe':
print 'invalid option, try again'
continue
else:
if choice == 'q':break
if choice == 'e':
src = raw_input('please input your source code:')
dst = raw_input('please input your destination code:')
string = raw_input('please input the string you want to transform:')
outstring = tr(src, dst, string)
print outstring
if __name__ == '__main__':
main()