Python 字典
1.字典和数据类型
mycat ={'size':'fat', 'name':'Vice', 'color':'gray'}
print("输出猫的名字为:", mycat['name'])
print('my cat name is '+mycat['name'])
2.字典与列表
spam=['dogs','cat','pig']
bacon = ['pig','cat','dogs']
print("spam=['dogs','cat','pig']")
print("bacon = ['pig','cat','dogs']")
if spam == bacon:
print("True")
else:
print('False')
egg = {'name':'jim', 'color':'gray'}
ham = {'color':'gray', 'name':'jim'}
print("egg = {'name':'jim', 'color':'gray'}")
print("ham = {'color':'gray', 'name':'jim'}")
if egg == ham:
print('True')
else:
print('False')
while True:
print('请输入egg中的值:',end=' ')
k = input()
try:
print(egg[k])
break
except KeyError:
print('输入的字典中没有')
3.keys()&values&items()使用方法
egg = {'name':'jim', 'color':'gray'}
for v in egg.values():
print("打印出值:", v)
print("\n")
for k in egg.keys():
print("打印出键:", k)
print("\n")
for i in egg.items():
print("打印出键-值:", i)
4.检查字典中的键或值
egg = {'name':'jim', 'color':'gray'}
"name" in egg.keys()
'gray' in egg.values()
5.get()使用
spam={'name':'jim', 'color':'gray'}
print('获取Name的值:', spam.get('name', 0))
print('输入需要查找的键:', end=' ')
j = input()
k =spam.get(j, 0)
if k == 0:
print('字典中没有'+str(j)+'的值')
else:
print('键'+str(j)+'的值为:', k)
6.setdefault()使用:
1:
spam={'name':'jim', 'color':'gray'}
while True:
print('输入需要查找的键:', end=' ')
j = input()
k =spam.get(j, 0)
if k == 0:
print('字典中没有'+str(j)+'的值')
print('输入'+str(j)+'的值:', end='')
i =input()
spam.setdefault(j, i)
print('输出字典spam:', spam)
else:
print('键'+str(j)+'的值为:', k)
break
2:
message = "John Smith was a good math student at a high school. He loved his computer. He came home early every day. Then he worked with it till midnight. But John was not a good English student, not good at all. He got an F in his English class. One day after school, John joined his computer to the computer in his high school office. The school office computer had the grades of all the students: the math grades, the science grades, the grades in arts and music, and grades in English. He found his English grade. An F! John changed his English grade from F to A. John’s parents looked at his report card. They were very happy. "
count={}
for char in message:
count.setdefault(char, 0)
count[char] = count[char]+1
print(count)
7.漂亮的打印pprint
import pprint
v = "John Smith was a good math student at a high school. He loved his computer. He came home early every day. Then he worked with it till midnight. But John was not a good English student, not good at all. He got an F in his English class. One day after school, John joined his computer to the computer in his high school office. The school office computer had the grades of all the students: the math grades, the science grades, the grades in arts and music, and grades in English. He found his English grade. An F! John changed his English grade from F to A. John’s parents looked at his report card. They were very happy. "
count={}
for char in v:
count.setdefault(char, 0)
count[char] = count[char]+1
pprint.pprint(count)
8.#字棋
the = {'top-L':'','top-M':' ','top-R':' ',
'mid-L':' ','mid-M':'','mid-R':' ',
'low-L':' ','low-M':' ','low-R':''}
print(the)
def bord(eh):
print(eh['top-L']+'|'+eh['top-M']+'|'+eh['top-R'])
print('-+-+-')
print(eh['mid-L']+'|'+eh['mid-M']+'|'+eh['mid-R'])
print('-+-+-')
print(eh['low-L']+'|'+eh['low-M']+'|'+eh['low-R'])
turn = "X"
for i in range(9):
bord(the)
print('Turn for ' + turn+' . Move on which space?')
move =input()
the[move]= turn
if turn == 'X':
turn = "O"
else:
turn = "X"
bord(the)
9.嵌套字典列表
the = {'Alice':{'apples':5, 'pies':12},
'Bob':{'cup':3, 'apples':2},
'Jim':{'cup':6, 'pies':7}
}
def tho(guess, item):
num = 0
for k, v in the.items():
num += v.get(item, 0)
print('k的值:', k)
print("v的值:", v)
return num
print('Number of things being brought:')
print('- apples '+str(tho(the, 'apples')))
print('- cup ' +str(tho(the, 'cup')))
print('- pies '+str(tho(the, 'pies')))
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。