使用 fromkeys 创建可变对象和字典
使用 dict.fromkeys 创建具有可变对象(例如列表)的字典时观察到的行为,一开始可能会令人惊讶。以下示例演示了该问题:
<code class="python">xs = dict.fromkeys(range(2), []) xs[0].append(1) print(xs) # Outputs: {0: [1], 1: [1]}</code>
在本例中,我们创建一个包含两个键(0 和 1)的字典,每个值都有一个空列表。但是,当我们将一个元素追加到与键 0 关联的列表时,它也会出现在与键 1 关联的列表中。
要理解此行为,请务必注意 dict.fromkeys 共享相同的值对象所有键之间。在我们的示例中,xs[0] 和 xs[1] 都指向同一个列表对象,如下所示:
<code class="python">print(xs[0] is xs[1]) # Outputs: True</code>
因此,通过 xs[0] 对列表所做的任何修改也会反映出来在 xs[1] 中,因为它们引用相同的底层对象。
相反,使用字典理解创建包含可变对象的字典会导致每个值都是一个单独的对象:
<code class="python">xs = {i: [] for i in range(2)} xs[0].append(1) print(xs) # Outputs: {0: [1], 1: []}</code>
在这种情况下,xs[0]和xs[1]不是同一个对象,因此修改xs[0]不会影响xs[1]。
在Python 2.6或更早版本中,当字典推导式不可用,您可以使用带有 dict() 的生成器表达式来实现与字典理解相同的行为:
<code class="python">xs = dict((i, []) for i in range(2))</code>
以上是为什么具有可变值的'dict.fromkeys”会创建共享对象?的详细内容。更多信息请关注PHP中文网其他相关文章!

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供应模拟性和可读性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

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

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

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