python section-5 字典

 # 字典 no顺序。列表have顺序

格式 {             }
#get方法防报错
#setdefault方法防治报错,无项目则添加有项目则不变其参数

#in 或not in方法检查是否存在键值

#get方法检查键是否存在,没有则返回0,不报错

value :值                 key:键                 item:   键---值对

sp.get('abc',0)   #  0是参数不存在时返回的值

漂亮打印:需要导入pprint模块

PrettyPrinter支持一下几种方法:

        pprint.pformat(object[, indent[, width[, depth]]])

        返回一个格式化的字符串

        pprint.pprint(object[, stream[, indent[, width[, depth]]]])

# 字典 no顺序。列表have顺序
#get方法防报错
#setdefault方法防治报错,无项目则添加有项目则不变其参数
mycat ={'size': 'fat','color':'green','loud':'big'}
print(mycat['size'])

sp={12:'dir',8:'vv'}
for v in sp.values():   
    print(v)              

a='abc'  in sp.values()
print(a)
b=str(sp.get('abc',0))  #0是备用值
print(b)         # end is 0 
sp.setdefault('he','fbi')
print(sp)                     #{12: 'dir', 8: 'vv', 'he': 'fbi'}
sp.setdefault('he','ttt')
print(sp)                      #{12: 'dir', 8: 'vv', 'he': 'fbi'}

end 
dir
vv
False

 
 
for v in sp.keys():
    print(v)
   

"""end  
12
8
 
 """
 
for v in sp.items():
    print(v)
 

 
(12, 'dir')
(8, 'vv')
"""





 
>>> mycat ={'size': 'fat','color':'green','loud':'big'}
>>> ca ={'color':'green','loud':'big','size': 'fat'}
>>> mycat==ca
True

>>> cat=[ 'green', 'big', 'fat']
>>> ca =[ 'big', 'fat','green']
>>> cat==ca
False

 








"""
tb={'top-l':' ','top-m':' ','top-r':' ' ,'mid-l':' ','mid-m':' ','mid-r':' ',  'low-l':' ','low-m':' ','low-r':' ' }

 
def printboard(board):
    print(board['top-l']+'|'+board['top-m']+'|'+board['top-r'] )
    print('-+-+-')
    print(board['mid-l']+'|'+board['mid-m']+'|'+board['mid-r'] )
    print('-+-+-')
    print(board['low-l']+'|'+board['low-m']+'|'+board['low-r'] )

turn='X'
for i in range(9):
    printboard(tb)
    print('turn for'+turn+'.move spece ?')
    move=input()
    if move=='7' :
        move='top-l'
    if move=='8' :
        move='top-m' 
    if move=='9' :
        move='top-r' 

    if move=='4' :
        move='mid-l'
    if move=='5' :
        move='mid-m' 
    if move=='6' :
        move='mid-r'  


    if move=='1' :
        move='low-l'
    if move=='2' :
        move='low-m' 
    if move=='3' :
        move='low-r'     
    
    tb[move]=turn
    if turn =='X' :
        turn='0'
    
    else :
        turn ='X'
printboard(tb)

"""
'''
 | |
-+-+-
 | |
-+-+-
 | |
'''

 






     

 











 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值