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

        <bdo id='HGg0s'></bdo><ul id='HGg0s'></ul>
      <legend id='HGg0s'><style id='HGg0s'><dir id='HGg0s'><q id='HGg0s'></q></dir></style></legend>

        从交替的侧面循环列表

        Cycle a list from alternating sides(从交替的侧面循环列表)

      1. <small id='FiibA'></small><noframes id='FiibA'>

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

              <tbody id='FiibA'></tbody>
              <bdo id='FiibA'></bdo><ul id='FiibA'></ul>
                • <legend id='FiibA'><style id='FiibA'><dir id='FiibA'><q id='FiibA'></q></dir></style></legend>

                  <tfoot id='FiibA'></tfoot>
                • 本文介绍了从交替的侧面循环列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  给定一个列表

                  a = [0,1,2,3,4,5,6,7,8,9]

                  我怎样才能得到

                  b = [0,9,1,8,2,7,3,6,4,5]

                  也就是说,产生一个新的列表,其中每个连续的元素都是从原始列表的两侧交替取出的?

                  解决方案

                  >>>[a[-i//2] if i % 2 else a[i//2] for i in range(len(a))][0, 9, 1, 8, 2, 7, 3, 6, 4, 5]

                  解释:
                  此代码从 a<的开头 (a[i//2]) 和结尾 (a[-i//2]) 选择数字/code>,交替(if i%2 else).总共选择了 len(a) 个数字,因此即使 len(a) 是奇数,这也不会产生不良影响.
                  [-i//2 for i in range(len(a))] 产生 0, -1, -1, -2, -2, -3, -3, -4, -4, -5,
                  [ i//2 for i in range(len(a))] 产生 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
                  i%2FalseTrue 之间交替,
                  所以我们从 a 中提取的索引是:0, -1, 1, -2, 2, -3, 3, -4, 4, -5.
                  p>

                  我对 pythonicness 的评价:
                  这个单线的好处是它很短并且显示出对称性(+i//2-i//2).
                  但不好的是,这种对称性具有欺骗性:
                  有人可能会认为 -i//2i//2 相同,只是符号翻转了.但在 Python 中,整数除法返回底数 的结果,而不是向零截断.所以 -1//2 == -1.
                  此外,我发现通过索引访问列表元素的方式比迭代更少.

                  Given a list

                  a = [0,1,2,3,4,5,6,7,8,9]
                  

                  how can I get

                  b = [0,9,1,8,2,7,3,6,4,5]
                  

                  That is, produce a new list in which each successive element is alternately taken from the two sides of the original list?

                  解决方案

                  >>> [a[-i//2] if i % 2 else a[i//2] for i in range(len(a))]
                  [0, 9, 1, 8, 2, 7, 3, 6, 4, 5]
                  

                  Explanation:
                  This code picks numbers from the beginning (a[i//2]) and from the end (a[-i//2]) of a, alternatingly (if i%2 else). A total of len(a) numbers are picked, so this produces no ill effects even if len(a) is odd.
                  [-i//2 for i in range(len(a))] yields 0, -1, -1, -2, -2, -3, -3, -4, -4, -5,
                  [ i//2 for i in range(len(a))] yields 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
                  and i%2 alternates between False and True,
                  so the indices we extract from a are: 0, -1, 1, -2, 2, -3, 3, -4, 4, -5.

                  My assessment of pythonicness:
                  The nice thing about this one-liner is that it's short and shows symmetry (+i//2 and -i//2).
                  The bad thing, though, is that this symmetry is deceptive:
                  One might think that -i//2 were the same as i//2 with the sign flipped. But in Python, integer division returns the floor of the result instead of truncating towards zero. So -1//2 == -1.
                  Also, I find accessing list elements by index less pythonic than iteration.

                  这篇关于从交替的侧面循环列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                  • <tfoot id='9C1V1'></tfoot>
                      <tbody id='9C1V1'></tbody>
                  • <small id='9C1V1'></small><noframes id='9C1V1'>

                          <bdo id='9C1V1'></bdo><ul id='9C1V1'></ul>

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