关于排序和字典 [英] about sort and dictionary
问题描述
对以下代码感到困惑:
a
[6,3,1] b
[4,3,1] c
{1:[[6,3,1],[4,3,1]],2:[[6,3,1]]} c [2] .append(b.sort())
c
{1:[[6,3,1],[1,3,4]],2:[[6,3,1],没有]}
#why c无法附加已排序的b ?? b.sort()
b
a [6, 3, 1] b [4, 3, 1] c {1: [[6, 3, 1], [4, 3, 1]], 2: [[6, 3, 1]]} c[2].append(b.sort())
c {1: [[6, 3, 1], [1, 3, 4]], 2: [[6, 3, 1], None]}
#why c can not append the sorted b?? b.sort()
b
[1,3,4]
[1, 3, 4]
推荐答案
施穆写道:
Shi Mu wrote:
对以下代码感到困惑:
a [6,3,1] b [4,3,1] c {1:[[6,3,1],[4,3,1]],2:[[6,3,1]] } c [2] .append(b.sort())
c {1:[[6,3,1],[1,3,4]],2:[[6,3,1] ,没有]}
#why c无法追加排序的b ?? b.sort()
b
a [6, 3, 1] b [4, 3, 1] c {1: [[6, 3, 1], [4, 3, 1]], 2: [[6, 3, 1]]} c[2].append(b.sort())
c {1: [[6, 3, 1], [1, 3, 4]], 2: [[6, 3, 1], None]}
#why c can not append the sorted b?? b.sort()
b
[1,3,4]
[1, 3, 4]
最内置的函数/方法don'不返回对象但没有。这个
我相信是语言创造者对所有内容的偏好。
显式。你最好这样做:
b.sort()
c [2] .append(b)
当然,这使得这样的事情变得不可能:
obj.method_a()。method_b()。method_c()
但语言(和社区)一般不鼓励你写这样的b $ b代码;-)
most built-in function/method don''t return the "object" but None. This
I believe is the language creator''s preference for everything being
explicit. You better do it like this :
b.sort()
c[2].append(b)
Of course, this make things like this not possible :
obj.method_a().method_b().method_c()
But the language(and the community) in general discourage you to write
code like this ;-)
施穆写道:
Shi Mu wrote:
对以下代码感到困惑:
a [6,3,1] b [ 4,3,1] c {1:[[6,3,1],[4,3,1]],2:[[6,3,1]]} c [2] .append(b.sort ())
c {1:[[6,3,1],[1,3,4]],2:[[6,3,1],无]}
#why c不能附加排序的b ?? b.sort()
b
a [6, 3, 1] b [4, 3, 1] c {1: [[6, 3, 1], [4, 3, 1]], 2: [[6, 3, 1]]} c[2].append(b.sort())
c {1: [[6, 3, 1], [1, 3, 4]], 2: [[6, 3, 1], None]}
#why c can not append the sorted b?? b.sort()
b
[1,3,4]
[1, 3, 4]
最内置的函数/方法don'不返回对象但没有。这个
我相信是语言创造者对所有内容的偏好。
显式。你最好这样做:
b.sort()
c [2] .append(b)
当然,这使得这样的事情变得不可能:
obj.method_a()。method_b()。method_c()
但语言(和社区)一般不鼓励你写这样的b $ b代码;-)
most built-in function/method don''t return the "object" but None. This
I believe is the language creator''s preference for everything being
explicit. You better do it like this :
b.sort()
c[2].append(b)
Of course, this make things like this not possible :
obj.method_a().method_b().method_c()
But the language(and the community) in general discourage you to write
code like this ;-)
Shi Mu< ; SA ************ @ gmail.com>写道:
Shi Mu <sa************@gmail.com> writes:
#why c无法附加已排序的b ??
#why c can not append the sorted b??
因为sort()没有返回什么?
根据图书馆参考:
7)sort()和reverse()方法修改了<在排序或撤销大型清单时,空间经济性为
。为了提醒你
他们通过副作用进行操作,他们不会返回已排序或
的反向清单。
-
Eric Jacoboni,ne il ya 1435934131 secondes
Because sort() doesn''t return anything?
According to the library reference:
7) The sort() and reverse() methods modify the list in place for
economy of space when sorting or reversing a large list. To remind you
that they operate by side effect, they don''t return the sorted or
reversed list.
--
Eric Jacoboni, ne il y a 1435934131 secondes
这篇关于关于排序和字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!