'''字典的生成式,zip()为打包函数''' items=['Fruits','Books','Others'] prices=[96,78,100] d={item.upper():price for item,price in zip(items,prices)}#upper()为大写字母函数 print(d)
E:\PycharmProjects\pythonProject\venv\Scripts\python.exe E:/PycharmProjects/pythonProject/demon1/demo8.py {'FRUITS': 96, 'BOOKS': 78, 'OTHERS': 100} 进程已结束,退出代码0
#如果两列生成列表一长一短,取短生成字典
'''字典的生成式,zip()为打包函数''' items=['Fruits','Books','Others'] prices=[96,78,100,101] #比item多一个元素 d={item.upper():price for item,price in zip(items,prices)}#upper()为大写字母函数 print(d)
E:\PycharmProjects\pythonProject\venv\Scripts\python.exe E:/PycharmProjects/pythonProject/demon1/demo8.py {'FRUITS': 96, 'BOOKS': 78, 'OTHERS': 100} 进程已结束,退出代码0