list.sort()对列表进行排序并保存已排序的列表,同时sorted(list)返回列表的已排序副本,而不更改原始列表.
list.sort()吗?是否有Python设计决策(PEP)阻止将已排序的容器添加到Python中?
(OrderedDict不是已排序的容器,因为它是按插入顺序排序的.)
我试图对ints 的Python列表进行排序,然后使用该.pop()函数返回最高的一个.我尝试过以不同的方式编写方法:
def LongestPath(T):
paths = [Ancestors(T,x) for x in OrdLeaves(T)]
#^ Creating a lists of lists of ints, this part works
result =[len(y) for y in paths ]
#^ Creating a list of ints where each int is a length of the a list in paths
result = result.sort()
#^meant to sort the result
return result.pop()
#^meant to return the largest int in the list (the last one)
Run Code Online (Sandbox Code Playgroud)
我也试过了
def LongestPath(T):
return[len(y) for y in …Run Code Online (Sandbox Code Playgroud) 当我试图找出我的BeautifulSoup网络刮刀的低价和高价时,我收到此错误.我附上了以下代码.我的列表不应该是一个整数列表吗?
我在发布之前经历了类似的NoneType问题,但解决方案没有用(或者我可能不理解它们!)
Traceback (most recent call last):
File "/home/user-machine/Desktop/cl_phones/main.py", line 47, in <module>
print "Low: $" + intprices[0]
TypeError: 'NoneType' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)
相关代码段:
intprices = []
newprices = prices[:]
total = 0
for k in newprices:
total += int(k)
intprices.append(int(k))
avg = total/len(newprices)
intprices = intprices.sort()
print "Average: $" + str(avg)
print "Low: $" + intprices[0]
print "High: $" + intprices[-1]
Run Code Online (Sandbox Code Playgroud) 我对导致这种情况的思维过程感兴趣.对我来说,一个相对新手,似乎阻碍了,因为它阻止了列表处理的"链接"(例如mylist.reverse().append('a string')[:someLimit]).我想可能是"那些人的力量"决定列表理解是一个更好的范例(一个有效的观点),所以不想鼓励其他方法 - 但是这似乎有悖常理,以防止一种直观的方法,即使更好存在替代品.
请注意,我不是在抱怨(我敢肯定有是一个合理的理由,我在它是什么只是感兴趣!),也不是寻找一个解决方案(意见在这里非常具有启发性) -只是寻找一些情况下,并深入理解语言的设计过程.
y = [1, 3, 2, 4]
x = y.sort()
print(x)
Run Code Online (Sandbox Code Playgroud)
x 是None。为什么这种语法适用于例如 Javascript 而不是 Python?
我想生成对称零对角矩阵。我的对称部分工作,但是当我使用 numpy 的 fill_diagonal 作为结果时,我得到了“无”。我的代码如下。感谢您阅读
import numpy as np
matrix_size = int(input("Size of the matrix \n"))
random_matrix = np.random.random_integers(-4,4,size=(matrix_size,matrix_size))
symmetric_matrix = (random_matrix + random_matrix.T)/2
print(symmetric_matrix)
zero_diogonal_matrix = np.fill_diagonal(symmetric_matrix,0)
print(zero_diogonal_matrix)
Run Code Online (Sandbox Code Playgroud) 码:
import math
import time
import random
class SortClass(object):
def sort1(self, l):
if len(l)==1:
return l
elif len(l)==2:
if l[0]<l[1]:
return l
else:
return l[::-1]
else:
pivot=math.floor(len(l)/2)
a=l[pivot:]
b=l[:pivot]
a2=self.sort1(a)
b2=self.sort1(b)
if a2==None or b2==None:
a2=[]
b2=[]
return (a2+b2).sort()
return []
Sort=SortClass()
x=[20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]
print(Sort.sort1(x))
Run Code Online (Sandbox Code Playgroud)
代码输出None,即使它应该在两种情况下返回一个空列表:
return []
Run Code Online (Sandbox Code Playgroud)
和
a2=self.mergeSort(a)
b2=self.mergeSort(b)
if a2==None or b2==None:
a2=[]
b2=[]
return (a2+b2).sort()
Run Code Online (Sandbox Code Playgroud)
详细信息:代码用于我为python练习制作的列表排序模块(我在python中相对较新).sort1是一个修改过的mergesort.
我试图在python中对对象列表进行排序,但是这段代码不起作用:
import datetime
class Day:
def __init__(self, date, text):
self.date = date
self.text = text
def __cmp__(self, other):
return cmp(self.date, other.date)
mylist = [Day(datetime.date(2009, 01, 02), "Jan 2"), Day(datetime.date(2009, 01, 01), "Jan 1")]
print mylist
print mylist.sort()
Run Code Online (Sandbox Code Playgroud)
这个输出是:
[<__main__.Day instance at 0x519e0>, <__main__.Day instance at 0x51a08>]
None
Run Code Online (Sandbox Code Playgroud)
有人能告诉我解决这个问题的好方法吗?为什么sort()函数返回None?