• <small id='a8gDF'></small><noframes id='a8gDF'>

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

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

      1. `sorted(list)` 与 `list.sort()` 有什么区别?

        时间:2023-08-31

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

              <tbody id='Vyzyo'></tbody>

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

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

                  本文介绍了`sorted(list)` 与 `list.sort()` 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  list.sort() 对列表进行排序并替换原始列表,而 sorted(list) 返回列表的排序副本,而不更改原始列表.

                  list.sort() sorts the list and replaces the original list, whereas sorted(list) returns a sorted copy of the list, without changing the original list.

                  • 什么时候比另一个更受青睐?
                  • 哪个更有效率?多少?
                  • list.sort() 执行后列表能否恢复为未排序状态?
                  • When is one preferred over the other?
                  • Which is more efficient? By how much?
                  • Can a list be reverted to the unsorted state after list.sort() has been performed?

                  推荐答案

                  sorted() 返回一个 new 排序列表,不影响原始列表.list.sort() 对列表进行 就地 排序,改变列表索引,并返回 None(与所有就地操作一样).

                  sorted() returns a new sorted list, leaving the original list unaffected. list.sort() sorts the list in-place, mutating the list indices, and returns None (like all in-place operations).

                  sorted() 适用于任何可迭代对象,而不仅仅是列表.字符串、元组、字典(你会得到键)、生成器等,返回一个包含所有元素的列表,排序.

                  sorted() works on any iterable, not just lists. Strings, tuples, dictionaries (you'll get the keys), generators, etc., returning a list containing all elements, sorted.

                  • 当你想要改变列表时使用list.sort(),当你想要一个新的排序对象返回时使用sorted().sorted() 当你想对一个可迭代的东西进行排序时,使用 .

                  • Use list.sort() when you want to mutate the list, sorted() when you want a new sorted object back. Use sorted() when you want to sort something that is an iterable, not a list yet.

                  对于列表,list.sort()sorted() 快,因为它不必创建副本.对于任何其他可迭代对象,您别无选择.

                  For lists, list.sort() is faster than sorted() because it doesn't have to create a copy. For any other iterable, you have no choice.

                  不,您无法检索原始位置.一旦你调用了 list.sort(),原来的顺序就消失了.

                  No, you cannot retrieve the original positions. Once you called list.sort() the original order is gone.

                  这篇关于`sorted(list)` 与 `list.sort()` 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                    <legend id='2gqi4'><style id='2gqi4'><dir id='2gqi4'><q id='2gqi4'></q></dir></style></legend>
                      <tbody id='2gqi4'></tbody>
                    <tfoot id='2gqi4'></tfoot>

                      • <bdo id='2gqi4'></bdo><ul id='2gqi4'></ul>

                        <small id='2gqi4'></small><noframes id='2gqi4'>