理解复杂的拆包表达式
考虑以下表达式:
a, b = 1, 2 # 简单序列赋值 a, b = ['green', 'blue'] # 列表赋值 a, b = 'XY' # 字符串赋值 a, b = range(1,5,2) # 任何可迭代对象都可以 # 嵌套序列赋值 (a,b), c = "XY", "Z" # a = 'X', b = 'Y', c = 'Z' (a,b), c = "XYZ" # 错误 -- 太多值需要拆包 (a,b), c = "XY" # 错误 -- 需要拆包的数值太少 (a,b), c, = [1,2],'this' # a = '1', b = '2', c = 'this' (a,b), (c,) = [1,2],'this' # 错误 -- 太多值需要拆包 # 扩展序列拆包 a, *b = 1,2,3,4,5 # a = 1, b = [2,3,4,5] *a, b = 1,2,3,4,5 # a = [1,2,3,4], b = 5 a, *b, c = 1,2,3,4,5 # a = 1, b = [2,3,4], c = 5 a, *b = 'X' # a = 'X', b = [] *a, b = 'X' # a = [], b = 'X' a, *b, c = "XY" # a = 'X', b = [], c = 'Y' a, *b, c = "X...Y" # a = 'X', b = ['.','.','.'], c = 'Y' a, b, *c = 1,2,3 # a = 1, b = 2, c = [3] a, b, c, *d = 1,2,3 # a = 1, b = 2, c = 3, d = [] a, *b, c, *d = 1,2,3,4,5 # 错误 -- 赋值中出现了两个星号表达式 (a,b), c = [1,2],'this' # a = '1', b = '2', c = 'this' (a,b), *c = [1,2],'this' # a = '1', b = '2', c = ['this'] (a,b), c, *d = [1,2],'this' # a = '1', b = '2', c = 'this', d = [] (a,b), *c, d = [1,2],'this' # a = '1', b = '2', c = [], d = 'this' (a,b), (c, *d) = [1,2],'this' # a = '1', b = '2', c = 't', d = ['h', 'i', 's'] *a = 1 # 错误 -- 目标必须在一个列表或元组中 *a = (1,2) # 错误 -- 目标必须在一个列表或元组中 *a, = (1,2) # a = [1,2] *a, = 1 # 错误 -- 'int' 对象不可迭代 *a, = [1] # a = [1] *a = [1] # 错误 -- 目标必须在一个列表或元组中 *a, = (1,) # a = [1] *a, = (1) # 错误 -- 'int' 对象不可迭代 *a, b = [1] # a = [], b = 1 *a, b = (1,) # a = [], b = 1 (a,b),c = 1,2,3 # 错误 -- 太多值需要拆包 (a,b), *c = 1,2,3 # 错误 - 'int' 对象不可迭代 (a,b), *c = 'XY', 2, 3 # a = 'X', b = 'Y', c = [2,3] # 扩展序列拆包 -- 嵌套 (a,b),c = 1,2,3 # 错误 -- 太多值需要拆包 *(a,b), c = 1,2,3 # a = 1, b = 2, c = 3 *(a,b) = 1,2 # 错误 -- 目标必须在一个列表或元组中 *(a,b), = 1,2 # a = 1, b = 2 *(a,b) = 'XY' # 错误 -- 目标必须在一个列表或元组中 *(a,b), = 'XY' # a = 'X', b = 'Y' *(a, b) = 'this' # 错误 -- 目标必须在一个列表或元组中 *(a, b), = 'this' # 错误 -- 拆包的值太多了 *(a, *b), = 'this' # a = 't', b = ['h', 'i', 's'] *(a, *b), c = 'this' # a = 't', b = ['h', 'i'], c = 's' *(a,*b), = 1,2,3,3,4,5,6,7 # a = 1, b = [2, 3, 3, 4, 5, 6, 7] *(a,*b), *c = 1,2,3,3,4,5,6,7 # 错误 -- 赋值中出现了两个星号表达式 *(a,*b), (*c,) = 1,2,3,3,4,5,6,7 # 错误 -- 'int' 对象不可迭代 *(a,*b), c = 1,2,3,3,4,5,6,7 # a = 1, b = [2, 3, 3, 4, 5, 6], c = 7 *(a,*b), (*c,) = 1,2,3,4,5,'XY' # a = 1, b = [2, 3, 4, 5], c = ['X', 'Y'] *(a,*b), c, d = 1,2,3,3,4,5,6,7 # a = 1, b = [2, 3, 3, 4, 5], c = 6, d = 7 *(a,*b), (c, d) = 1,2,3,3,4,5,6,7 # 错误 -- 'int' 对象不可迭代 *(a,*b), (*c, d) = 1,2,3,3,4,5,6,7 # 错误 -- 'int' 对象不可迭代 *(a,*b), *(c, d) = 1,2,3,3,4,5,6,7 # 错误 -- 赋值中出现了两个星号表达式 *(a,b), c = 'XY', 3 # 错误 -- 需要拆包的数值太少 *(*a,b), c = 'XY', 3 # a = [], b = 'XY', c = 3 (a,b), c = 'XY', 3 # a = 'X', b = 'Y', c = 3 *(a,b), c = 'XY', 3, 4 # a = 'XY', b = 3, c = 4 *(*a,b), c = 'XY', 3, 4 # a = ['XY'], b = 3, c = 4 (a,b), c = 'XY', 3, 4 # 错误 -- 拆包的值太多了
手动推导出这些表达式的结果
掌握一些基本规则后,对其进行概括并不难。我将尽我最大的努力通过一些示例进行解释。由于你提到了“手动”计算这些表达式,我会建议一些简单的替换规则。基本上,你会发现理解所有可迭代对象以相同方式格式化的表达式会更容易一些。
仅为拆包目的,以下替换在右值(即可迭代对象)中有效:
'XY' -> ('X', 'Y') ['X', 'Y'] -> ('X', 'Y')
如果你发现某个值未被拆包,那么你需要撤销该替换。(有关进一步的解释,请参见下文。)
此外,当你看到“裸露的”逗号时,想象最顶层有一个元组。在左右两侧(即可迭代对象和右可迭代对象)执行此操作:
'X', 'Y' -> ('X', 'Y') a, b -> (a, b)
以上是如何理解Python中复杂的解包表达式?的详细内容。更多信息请关注PHP中文网其他相关文章!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,负索引,切片,口头化。1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器