


dictionary
will accompany you in your subsequent study and work. Whether the dictionary is unordered
>>> # Python 2.7 >>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> # Python 3.5 >>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
Key value swap
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {} >>> for key, value in a_dict.items(): ... new_dict[value] = key ... >>> new_dict {1: 'one', 2: 'two', 3: 'thee', 4: 'four'}
Filter the dictionary based on certain conditions
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {} # Create a new empty dictionary >>> for key, value in a_dict.items(): ... if value <= 2: ... new_dict[key] = value ... >>> new_dict {'one': 1, 'two': 2}
利用字典中的值,做一些计算
>>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00} >>> total_income = 0.00 >>> for value in incomes.values(): ... total_income += value # Accumulate the values in total_income ... >>> total_income 14100.0
字典推导式
>>> objects = ['blue', 'apple', 'dog'] >>> categories = ['color', 'fruit', 'pet'] >>> a_dict = {key: value for key, value in zip(categories, objects)} >>> a_dict {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
利用字典推导式,实现键值转换
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {value: key for key, value in a_dict.items()} >>> new_dict {1: 'one', 2: 'two', 3: 'thee', 4: 'four'}
利用字典推导式,过滤字典
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {k: v for k, v in a_dict.items() if v <= 2} >>> new_dict {'one': 1, 'two': 2}
利用字典推导式,做一些计算
>>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00} >>> total_income = sum([value for value in incomes.values()]) >>> total_income 14100.0
字典排序
>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00} >>> sorted_income = {k: incomes[k] for k in sorted(incomes)} >>> sorted_income {'apple': 5600.0, 'banana': 5000.0, 'orange': 3500.0}
内置函数,与字典配合使用
map()函数
>>> prices = {'apple': 0.40, 'orange': 0.35, 'banana': 0.25} >>> def discount(current_price): ... return (current_price[0], round(current_price[1] * 0.95, 2)) ... >>> new_prices = dict(map(discount, prices.items())) >>> new_prices {'apple': 0.38, 'orange': 0.33, 'banana': 0.24}
filter()函数
>>> prices = {'apple': 0.40, 'orange': 0.35, 'banana': 0.25} >>> def has_low_price(price): ... return prices[price] < 0.4 ... >>> low_price = list(filter(has_low_price, prices.keys())) >>> low_price ['orange', 'banana']
字典解包运算符
>>> vegetable_prices = {'pepper': 0.20, 'onion': 0.55} >>> fruit_prices = {'apple': 0.40, 'orange': 0.35, 'pepper': .25} >>> {**vegetable_prices, **fruit_prices} {'pepper': 0.25, 'onion': 0.55, 'apple': 0.4, 'orange': 0.35}
The above is the detailed content of Tips | 11 'Python Dictionary' knowledge points that are easy to use and take off!. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)
