0
点赞
收藏
分享

微信扫一扫

定义一个字典dict1 = {“name”:”玛卡巴卡”,”phone”:”120”,”address”:”上海虹桥”} 整合字典,判断字典的值长度是否大于2,如果大于2则仅保留前两位并重新赋值给原

拾杨梅记 2022-10-11 阅读 154
def show_updata_list(self):
    dict1 = {"name":"玛卡巴卡","phone":"120","address":"上海虹桥"}
    for i,j in dict1.items():   #遍历字典键值对
        if len(j) > 2:
            dict1[i] = j[:2]   #存字典 字典名[键] = 值
    return dict1

调用函数 f6 = MyCar(a,b) print(f6.show_updata_list())

举报

相关推荐

0 条评论