<legend id='7W5m6'><style id='7W5m6'><dir id='7W5m6'><q id='7W5m6'></q></dir></style></legend>

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

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

        <bdo id='7W5m6'></bdo><ul id='7W5m6'></ul>
    1. <tfoot id='7W5m6'></tfoot>
    2. 从元组列表创建 Python 列表

      Creating a Python list from a list of tuples(从元组列表创建 Python 列表)
    3. <i id='4kItY'><tr id='4kItY'><dt id='4kItY'><q id='4kItY'><span id='4kItY'><b id='4kItY'><form id='4kItY'><ins id='4kItY'></ins><ul id='4kItY'></ul><sub id='4kItY'></sub></form><legend id='4kItY'></legend><bdo id='4kItY'><pre id='4kItY'><center id='4kItY'></center></pre></bdo></b><th id='4kItY'></th></span></q></dt></tr></i><div id='4kItY'><tfoot id='4kItY'></tfoot><dl id='4kItY'><fieldset id='4kItY'></fieldset></dl></div>
        <bdo id='4kItY'></bdo><ul id='4kItY'></ul>

        <small id='4kItY'></small><noframes id='4kItY'>

          <tbody id='4kItY'></tbody>

                <tfoot id='4kItY'></tfoot>
                <legend id='4kItY'><style id='4kItY'><dir id='4kItY'><q id='4kItY'></q></dir></style></legend>
                本文介绍了从元组列表创建 Python 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                例如,如果我有一个元组列表,例如

                If I have, for example, a list of tuples such as

                a = [(1,2)] * 4
                

                如何创建每个元组的第一个元素的列表?即[1, 1, 1, 1].

                how would I create a list of the first element of each tuple? That is, [1, 1, 1, 1].

                推荐答案

                使用 列表理解:

                >>> a = [(1,2)] * 4
                >>> [t[0] for t in a]
                [1, 1, 1, 1]
                

                你也可以解包元组:

                >>> [first for first,second in a]
                [1, 1, 1, 1]
                

                如果你想变得花哨,结合 mapoperator.itemgetter.在 python 3 中,您必须将构造包装在 list 中以获取列表而不是可迭代对象:

                If you want to get fancy, combine map and operator.itemgetter. In python 3, you'll have to wrap the construct in list to get a list instead of an iterable:

                >>> import operator
                >>> map(operator.itemgetter(0), a)
                <map object at 0x7f3971029290>
                >>> list(map(operator.itemgetter(0), a))
                [1, 1, 1, 1]
                

                这篇关于从元组列表创建 Python 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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='0pJNU'></tbody>

                <small id='0pJNU'></small><noframes id='0pJNU'>

                  <bdo id='0pJNU'></bdo><ul id='0pJNU'></ul>

                        <legend id='0pJNU'><style id='0pJNU'><dir id='0pJNU'><q id='0pJNU'></q></dir></style></legend>

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