首页 > 编程语言 >[998] Python unpacking operators (* and **)

[998] Python unpacking operators (* and **)

时间:2024-05-20 11:20:21浏览次数:19  
标签:apple Python operators 998 dict my unpacking

ref: Python unpacking operators (* and **) (RECOMMENDED)

ref: Python Functions

ref: Python Unpack Dictionary: A Comprehensive Guide


Here are some crucial things for take-away of the unpacking operators:

  • A single asterisk * unpacks items from lists, tuples, sets, and strings;
  • A double asterisk ** unpacks dictionaries;
  • Using a single asterisk on a dictionary will unpack only dictionary keys;
  • You can pass all iterable items to a function with the help of unpacking operators;
  • You can merge and update iterables using the starred expressions.

Some examples:

The double asterisk can merge several dictionaries:

dict_1 = {'a': 1, 'b': 2, 'c': 3}
dict_2 = {'one': 'two', 'three': 'four'}

dict_3 = {**dict_1, **dict_2}
# {'a': 1, 'b': 2, 'c': 3, 'one': 'two', 'three': 'four'}

You can merge not only multiple dictionaries but also add extra key-value pairs to the existing ones. For instance:

my_dict = {'apple': 1, 'banana': 2, 'pear': 3}
my_dict_updated = {**my_dict, 'strawberry': 4}
print(my_dict_updated)
# {'apple': 1, 'banana': 2, 'pear': 3, 'strawberry': 4}

Or even create a copy of a dictionary and update its values at the same time:

my_dict = {'apple': 1, 'banana': 2, 'pear': 3}
my_dict_copy = {**my_dict, 'apple': 100}
print(my_dict_copy)
# {'apple': 100, 'banana': 2, 'pear': 3}

 

标签:apple,Python,operators,998,dict,my,unpacking
From: https://www.cnblogs.com/alex-bn-lee/p/18201471

相关文章

  • python去除图片白边黑边
    主要用于去除图片的白边和黑边,比如在截图表情包的时候,通过小米的传送门保存图片的时候,图片往往会有黑边和白边,此时使用此脚本二次处理importosfromPILimportImage,ImageChopsdeftrim_white_border(image):bg=Image.new(image.mode,image.size,image.getpixel((0......
  • CSP历年复赛题-P1008 [NOIP1998 普及组] 三连击
    原题链接:https://www.luogu.com.cn/problem/P1008题意解读:将 1,2,…,9共 9个数分成3组,分别组成3个三位数,且使这 3 个三位数构成 1:2:3的比例,枚举所有的组合即可。解题思路:设定三个数a、b、c枚举a,最小123,最大987b=a*2,c=a*3判断b、c是否是三位数,且a、b、c中所......
  • CSP历年复赛题-P1009 [NOIP1998 普及组] 阶乘之和
    原题链接:https://www.luogu.com.cn/problem/P1009题意解读:  利用高精度计算阶乘之和,需要用到高精度乘法(高精度乘低精度)、高精度加法。  首先,思考不利用高精度如何解题,直观方法就是遍历i从1到n,每次乘i得到i的阶乘,然后累加到结果,代码如下:#include<bits/stdc++.h>usingnam......
  • CSP历年复赛题-P1010 [NOIP1998 普及组] 幂次方
    原题链接:https://www.luogu.com.cn/problem/P1010题意解读:输出一个正整数的2的幂次方表示,需要用到二进制数学知识,将整数拆解成2的次幂之和,幂次方也要进行拆解,因此容易想到通过递归处理。解题思路:先看样例,给定整数137,要拆解成2的幂次方之和,先考虑i使得刚好137>=2^i时,i取7,因此2......
  • Python内置库:pathlib(文件路径操作)
    官方文档:pathlib—Object-orientedfilesystempaths一、基础使用遍历子目录使用通配符遍历文件拼接路径获取标准化后的绝对路径查询路径常规属性打开文件frompathlibimportPathprint('1.1查询指定目录的子目录')p=Path('D:/Envs')print([sub_pforsub_p......
  • 【PYTHON3】环境搭建+编程学习之路的开始——Windows系统
    一、概述在学习python开发语言之前需要安装好开发语言环境(也就是常说的开发环境)开发环境主要有:解释器和编辑器IDE,而其中的解释器是用来将代码转换成机器语言,python语言也就是解释器;编辑器用来写代码逻辑,python语言推荐的是pycharm,它是IDE集成开发环境,这里面有开发时需要的工具......
  • 【开源】2024最新python豆瓣电影数据爬虫+可视化分析项目
    项目介绍【开源】项目基于python+pandas+flask+mysql等技术实现豆瓣电影数据获取及可视化分析展示,觉得有用的朋友可以来个一键三连,感谢!!!项目演示[video(video-C9B87WwE-1716106102936)(type-bilibili)(url-https://player.bilibili.com/player.html?aid=1204518067)(image-https......
  • Python __str__ 和 __repr__
    在Python中,__str__和__repr__是两个非常重要的特殊方法,它们用于定义对象的字符串表示形式,但它们的用途和场景稍有不同:__str__ 方法__str__方法提供了对象的“人性化”字符串表示,它的目的是为了方便人类阅读和理解。当你使用print()函数打印一个对象,或者直接将对象用在字......
  • Python 数据降级(重采样)
    在数据处理中,经常有高频数据转成低频,秒级数据转成分钟、小时数据等。我们将讨论以下方法:使用Pandas的resample方法:示例:将天数据转化成月数据。代码示例:importpandasaspdimportnumpyasnp#创建随机成绩score数据df=pd.DataFrame({'date':pd.date_range......
  • 百度 Apollo 自定义模块发布——使用 Python 语言(bazel 编译 Python 模块)_bazel-bin b
    CSDN搬家失败,手动导出markdown后再导入博客园BinaryvsComponent首先说明下,Apollo的核心概念是组件,通过组件可以实现资源的自动管理和调度。CyberRT中只能使用C++语言实现Component,Python版的API只能用来写传统的二进制可执行文件,参考官方文档中这两种方式的区别:B......