0
点赞
收藏
分享

微信扫一扫

表达式操作元组和字典

GG_lyf 2022-03-19 阅读 55
python

对元组内的内容 筛选后赋值给另一元组

字典  new_dirc={key:dic[key] for key in dirc if  条件语句}

#元组推导式
def test_():
    books=('程序员的必修课本','构建之法','代码大全','TCP/IP协议详解')
    reading=(book for book in books if len(book)<=4)
    print("太长的书就不看了,只读短的:")
    for book in reading:
        print(" ->《{}》".format(book))
    print("可是发现书的名字短,内容也可能很长啊!")
#字典推导式
def test2():
    install={
        'w':{'platform':'windows','desc': "请下载 Windows 安装包安装:https://www.python.org/downloads/windows/"},
        "l": {
            "platform": "Linux",
            "desc": "请下载 Linux 的 Python 源码安装:https://www.python.org/downloads/source/",
        },
        "m": {
            "platform": "MacOS",
            "desc": "请下载 Mac 的安装包:https://www.python.org/downloads/macos/,或者使用 brew install python 安装",
        }
    }
    # install--> w l  m --> platform , desc ,
    # 将除了w 之外的 字典 放入no_windows
    no_windows={key:install[key] for key in install  if key !='w'}
   # print(no_windows)
    for a in no_windows:
        print("安装平台:{}".format(no_windows[a]['platform']))
        print ("安装说明:{}".format(no_windows[a]['desc']))
test2()
举报

相关推荐

0 条评论