我有
b = heapify([5,4,9,1])
Run Code Online (Sandbox Code Playgroud)
如果我做了
type(b)
Run Code Online (Sandbox Code Playgroud)
它说它是NoneType而不是列表类型,任何人都知道我做错了什么?
我只是对以下代码片段不起作用感到好奇,该代码片段应初始化一个空列表,并在运行时追加一个初始值。第二个片段正在工作。它出什么问题了?是否有进行此类初始化的最佳实践?
>>> foo = [].append(2)
>>> print foo
None
>>> foo = []
>>> foo.append(2)
>>> print foo
[2]
Run Code Online (Sandbox Code Playgroud)
编辑:似乎我对语法有误解。如下面已经指出的,append总是返回None作为结果值。我首先想到的是[]将返回一个空的列表对象,在该对象的附加项中应添加第一个元素,然后将该列表分配给foo。错了
有什么办法可以替换以下内容:
names = ['john', 'mike', 'james']
names.append('dan')
print(names)
Run Code Online (Sandbox Code Playgroud)
使用与此类似的单行(将打印None)?:
names = ['john', 'mike', 'james'].append('dan')
print(names)
Run Code Online (Sandbox Code Playgroud) 我很困惑:
class lin_reg:
def __init__(self):
''' Executes the program '''
Indep_Array, Dep_Array = self.Prob_Def()
Total_Array = Indep_Array.append(Dep_Array)
print Indep_Array, Dep_Array, Total_Array
NumArray = len(Total_Array)
def Prob_Def(self):
Analy_Type = raw_input('Type of Regression(linear-n,nonlinear-nl): ')
Num_IndepVar = eval(raw_input('Number of Independent Variables: '))
Indep_Array = []
for IndepVar in range(Num_IndepVar):
ArrayInput = eval(raw_input('Enter the array: '))
Indep_Array.append(ArrayInput)
Dep_Array = eval(raw_input('Enter the dependent array: '))
return Indep_Array, Dep_Array
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我得到如下输出:
obs=lin_reg.lin_reg()
Type of Regression(linear-n,nonlinear-nl): nl
Number of Independent Variables: 3
Enter the array: [1,2,3]
Enter the …Run Code Online (Sandbox Code Playgroud) 我在Python中有一些代码,它应该以列表的形式将所有根目录返回到二叉树中的叶子路径(例如["1-> 2-> 5","1-> 3"]).原始问题来自leetcode.
我需要帮助找出我的代码和/或替代解决方案的问题(最好是Python).对于我上面给出的示例,我的代码返回null,而它实际上应该打印我给出的列表.当你第一次看到这个问题时,我也很感激你如何解决这个问题.
这是我的代码:
def binaryTreePaths(self, root):
list1 = []
if (root == None):
return []
if (root.left == None and root.right == None):
return list1.append(str(root.val) + "->")
if (root.left != None):
list1.append(self.binaryTreePaths(root.left))
if (root.right != None):
list1.append(self.binaryTreePaths(root.right))
Run Code Online (Sandbox Code Playgroud) 我是python编程的新手,为什么k的赋值给出'None'值
> >>> l=[2,3,4]
> >>> k=l.append(14)
> >>> print k
None
> >>> print l
[2, 3, 4, 14]
Run Code Online (Sandbox Code Playgroud)
在上面的示例List中,我将14值附加到列表然后分配给k但是k正在打印无请告诉我它打印的原因是否而不是附加列表?
谢谢mukthyar
基本上,我有一个列表,我想立即执行多个功能.例如,
List = [1,2,3,4,5]
List.extend([1,2,3,4,5]).sort().reverse()
Run Code Online (Sandbox Code Playgroud)
我希望结果如此[5,5,4,4,3,3,2,2,1,1].
我有一段时间没有使用过Python,但我知道我之前做过类似的事情.这是简单的我缺少什么或什么?
顺便说一句,它必须在一条线上.
我的函数总是返回None,这是怎么回事?
Az= [5,4,25.2,685.8,435,2,8,89.3,3,794]
new = []
def azimuth(a,b,c):
if c == list:
for i in c:
if i > a and i < b:
new.append(i)
return new
d=azimuth(10,300,Az)
print d
Run Code Online (Sandbox Code Playgroud)
此外,如果有人知道如何将这些数字的位置提取到不同的列表中,那将非常有帮助。