<legend id='K6nSo'><style id='K6nSo'><dir id='K6nSo'><q id='K6nSo'></q></dir></style></legend>

<small id='K6nSo'></small><noframes id='K6nSo'>

    <i id='K6nSo'><tr id='K6nSo'><dt id='K6nSo'><q id='K6nSo'><span id='K6nSo'><b id='K6nSo'><form id='K6nSo'><ins id='K6nSo'></ins><ul id='K6nSo'></ul><sub id='K6nSo'></sub></form><legend id='K6nSo'></legend><bdo id='K6nSo'><pre id='K6nSo'><center id='K6nSo'></center></pre></bdo></b><th id='K6nSo'></th></span></q></dt></tr></i><div id='K6nSo'><tfoot id='K6nSo'></tfoot><dl id='K6nSo'><fieldset id='K6nSo'></fieldset></dl></div>
  1. <tfoot id='K6nSo'></tfoot>
        <bdo id='K6nSo'></bdo><ul id='K6nSo'></ul>
    1. 如何从元组中解压缩可选项目?

      How to unpack optional items from a tuple?(如何从元组中解压缩可选项目?)
          • <legend id='fUfjz'><style id='fUfjz'><dir id='fUfjz'><q id='fUfjz'></q></dir></style></legend>
              <bdo id='fUfjz'></bdo><ul id='fUfjz'></ul>
                  <tbody id='fUfjz'></tbody>

                <tfoot id='fUfjz'></tfoot>
              1. <small id='fUfjz'></small><noframes id='fUfjz'>

                <i id='fUfjz'><tr id='fUfjz'><dt id='fUfjz'><q id='fUfjz'><span id='fUfjz'><b id='fUfjz'><form id='fUfjz'><ins id='fUfjz'></ins><ul id='fUfjz'></ul><sub id='fUfjz'></sub></form><legend id='fUfjz'></legend><bdo id='fUfjz'><pre id='fUfjz'><center id='fUfjz'></center></pre></bdo></b><th id='fUfjz'></th></span></q></dt></tr></i><div id='fUfjz'><tfoot id='fUfjz'></tfoot><dl id='fUfjz'><fieldset id='fUfjz'></fieldset></dl></div>
                本文介绍了如何从元组中解压缩可选项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一些输入值的列表,其中前一对是强制性的,最后一对是可选的.是否有任何简单的方法可以使用元组解包将这些分配给变量,如果缺少可选参数则为 None.

                I have a list of some input values, of which the first couple of mandatory and the last couple optional. Is there any easy way to use tuple unpacking to assign these to variables, getting None if the optional parameters are missing.

                例如.

                a = [1,2]   
                foo, bar, baz = a
                # baz == None
                

                理想情况下,a 可以是任意长度 - 包括超过 3 个(其他物品被丢弃).

                ideally a could be any length - including longer than 3 (other items thrown away).

                目前我正在使用带有参数名称列表的 zip 来获取字典:

                At the moment I'm using zip with a list of parameter names to get a dictionary:

                items = dict(zip(('foo', 'bar', 'baz'), a))
                foo = items.get('foo', None)
                bar = items.get('bar', None)
                baz = items.get('baz', None)
                

                不过有点啰嗦.

                推荐答案

                从链接的问题来看,这是可行的:

                From the linked question, this works:

                foo, bar, baz == (list(a) + [None]*3)[:3]
                

                这篇关于如何从元组中解压缩可选项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                      <tbody id='7bveL'></tbody>
                  • <legend id='7bveL'><style id='7bveL'><dir id='7bveL'><q id='7bveL'></q></dir></style></legend>

                    <tfoot id='7bveL'></tfoot>
                      <bdo id='7bveL'></bdo><ul id='7bveL'></ul>

                      <i id='7bveL'><tr id='7bveL'><dt id='7bveL'><q id='7bveL'><span id='7bveL'><b id='7bveL'><form id='7bveL'><ins id='7bveL'></ins><ul id='7bveL'></ul><sub id='7bveL'></sub></form><legend id='7bveL'></legend><bdo id='7bveL'><pre id='7bveL'><center id='7bveL'></center></pre></bdo></b><th id='7bveL'></th></span></q></dt></tr></i><div id='7bveL'><tfoot id='7bveL'></tfoot><dl id='7bveL'><fieldset id='7bveL'></fieldset></dl></div>

                          <small id='7bveL'></small><noframes id='7bveL'>