Python Numpy Tutorial: 字典
2017-05-08 16:36
169 查看
本文的内容:
字典的应用方法
字典的应用方法
# -*- coding: utf-8 -*- """ Python Version: 3.5 Created on Mon May 8 16:02:30 2017 E-mail: Eric2014_Lv@sjtu.edu.cn @author: DidiLv """ d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data print(d['cat']) # Get an entry from a dictionary; prints "cute" print('cat' in d) # Check if a dictionary has a given key; prints "True" d['fish'] = 'wet' # Set an entry in a dictionary print(d['fish']) # Prints "wet" print(d.get('monkey', 'N/A')) # Get an element with a default; prints "N/A" print(d.get('fish', 'N/A')) # Get an element with a default; prints "wet" del(d['fish']) # Remove an element from a dictionary print(d.get('fish', 'N/A')) # "fish" is no longer a key; prints "N/A" print("-------------------------------------------") d = {'person': 2, 'cat': 4, 'spider': 8} # method 1: for animal in d: legs = d[animal] print('A %s has %d legs' % (animal, legs)) # method 2: by using iteritems to find key and values for animal, legs in d.items(): print('A %s has %d legs' % (animal, legs)) # Note:In python 3.5, the dictionary has no iteritems term, they are iters()) # 字典解析 类似于列表解析 功能强大 dict_1 = {animal: legs for animal, legs in d.items()} print(dict_1)
相关文章推荐
- python 列表 字典 string 互相转换
- python_day3 — 字典
- 将一段用户信息用python字典打印出来
- python的各种推导式(列表推导式、字典推导式、集合推导式)
- Python:字典数据类型的一些知识
- python字典和列表各种嵌套排序
- python的【字典dict】:创建、访问、更新、删除;查看键、值、键值对;遍历;排序
- python 字典嵌套赋值
- Python 字典
- python全栈开发-Day5 元组、字典
- 【python学习笔记】Python数据结构(列表,元组,字典,集合)
- Python 字典(Dictionary)
- 跟老齐学Python之字典,你还记得吗?
- Python核心对象类型之列表与字典
- 详细探究Python中的字典容器
- Python中字典get方法的使用
- Python 语言及其应用 Chapter_3_Note_2 容器_列表_元组_字典_集合
- Python 字典
- Python学习笔记——字典
- python有序字典