<small id='6O6bs'></small><noframes id='6O6bs'>

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

      <tfoot id='6O6bs'></tfoot>

    2. <legend id='6O6bs'><style id='6O6bs'><dir id='6O6bs'><q id='6O6bs'></q></dir></style></legend>
        <bdo id='6O6bs'></bdo><ul id='6O6bs'></ul>
      1. 在 Python 中将多个变量附加到列表中

        时间:2023-09-27
      2. <tfoot id='JTn7E'></tfoot>
        <i id='JTn7E'><tr id='JTn7E'><dt id='JTn7E'><q id='JTn7E'><span id='JTn7E'><b id='JTn7E'><form id='JTn7E'><ins id='JTn7E'></ins><ul id='JTn7E'></ul><sub id='JTn7E'></sub></form><legend id='JTn7E'></legend><bdo id='JTn7E'><pre id='JTn7E'><center id='JTn7E'></center></pre></bdo></b><th id='JTn7E'></th></span></q></dt></tr></i><div id='JTn7E'><tfoot id='JTn7E'></tfoot><dl id='JTn7E'><fieldset id='JTn7E'></fieldset></dl></div>
          1. <legend id='JTn7E'><style id='JTn7E'><dir id='JTn7E'><q id='JTn7E'></q></dir></style></legend>

              <tbody id='JTn7E'></tbody>

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

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

                  本文介绍了在 Python 中将多个变量附加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想将几个变量附加到一个列表中.变量的数量不同.所有变量都以volume"开头.我在想也许是通配符或其他东西可以做到.但我找不到这样的东西.任何想法如何解决这个问题?请注意,在此示例中它是三个变量,但也可以是五个或六个或任何值.

                  I want to append several variables to a list. The number of variables varies. All variables start with "volume". I was thinking maybe a wildcard or something would do it. But I couldn't find anything like this. Any ideas how to solve this? Note in this example it is three variables, but it could also be five or six or anything.

                  volumeA = 100
                  volumeB = 20
                  volumeC = 10
                  
                  vol = []
                  
                  vol.append(volume*)
                  

                  推荐答案

                  您可以使用 extend 将任何可迭代对象附加到列表中:

                  You can use extend to append any iterable to a list:

                  vol.extend((volumeA, volumeB, volumeC))
                  

                  根据你的变量名的前缀对我来说有一种不好的代码味道,但你可以做到.(附加值的顺序未定义.)

                  Depending on the prefix of your variable names has a bad code smell to me, but you can do it. (The order in which values are appended is undefined.)

                  vol.extend(value for name, value in locals().items() if name.startswith('volume'))
                  

                  如果顺序很重要(恕我直言,仍然闻起来不对):

                  If order is important (IMHO, still smells wrong):

                  vol.extend(value for name, value in sorted(locals().items(), key=lambda item: item[0]) if name.startswith('volume'))
                  

                  这篇关于在 Python 中将多个变量附加到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:附加两个具有相同列、不同顺序的数据框 下一篇:将项目附加到列表列表中的指定列表(Python)

                  相关文章

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

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

                    1. <tfoot id='xm3hs'></tfoot>

                        <bdo id='xm3hs'></bdo><ul id='xm3hs'></ul>