• <tfoot id='ax6Zu'></tfoot>

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

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

        <legend id='ax6Zu'><style id='ax6Zu'><dir id='ax6Zu'><q id='ax6Zu'></q></dir></style></legend>
      1. 访问列表的多个元素知道它们的索引

        时间:2024-04-21
        <i id='Wj6Bj'><tr id='Wj6Bj'><dt id='Wj6Bj'><q id='Wj6Bj'><span id='Wj6Bj'><b id='Wj6Bj'><form id='Wj6Bj'><ins id='Wj6Bj'></ins><ul id='Wj6Bj'></ul><sub id='Wj6Bj'></sub></form><legend id='Wj6Bj'></legend><bdo id='Wj6Bj'><pre id='Wj6Bj'><center id='Wj6Bj'></center></pre></bdo></b><th id='Wj6Bj'></th></span></q></dt></tr></i><div id='Wj6Bj'><tfoot id='Wj6Bj'></tfoot><dl id='Wj6Bj'><fieldset id='Wj6Bj'></fieldset></dl></div>
        • <bdo id='Wj6Bj'></bdo><ul id='Wj6Bj'></ul>

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

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

              <tbody id='Wj6Bj'></tbody>
            <tfoot id='Wj6Bj'></tfoot>

                  本文介绍了访问列表的多个元素知道它们的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要从给定列表中选择一些元素,知道它们的索引.假设我想创建一个新列表,其中包含来自给定列表 [-2, 1, 5, 3, 8, 5, 6] 的索引为 1、2、5 的元素.我所做的是:

                  I need to choose some elements from the given list, knowing their index. Let say I would like to create a new list, which contains element with index 1, 2, 5, from given list [-2, 1, 5, 3, 8, 5, 6]. What I did is:

                  a = [-2,1,5,3,8,5,6]
                  b = [1,2,5]
                  c = [ a[i] for i in b]
                  

                  有没有更好的方法呢?类似 c = a[b] 的东西?

                  Is there any better way to do it? something like c = a[b] ?

                  推荐答案

                  你可以使用 operator.itemgetter:

                  You can use operator.itemgetter:

                  from operator import itemgetter 
                  a = [-2, 1, 5, 3, 8, 5, 6]
                  b = [1, 2, 5]
                  print(itemgetter(*b)(a))
                  # Result:
                  (1, 5, 5)
                  

                  或者你可以使用 numpy:

                  import numpy as np
                  a = np.array([-2, 1, 5, 3, 8, 5, 6])
                  b = [1, 2, 5]
                  print(list(a[b]))
                  # Result:
                  [1, 5, 5]
                  

                  <小时>

                  但实际上,您当前的解决方案很好.它可能是所有这些中最整洁的.


                  But really, your current solution is fine. It's probably the neatest out of all of them.

                  这篇关于访问列表的多个元素知道它们的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:什么是 setup.py? 下一篇:为什么 Python 3.6.1 抛出 AttributeError:模块 'enum' 没有属性 &#

                  相关文章

                  • <bdo id='dyr51'></bdo><ul id='dyr51'></ul>

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

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