为什么这样做 -
a = []
a.append(4)
print a
Run Code Online (Sandbox Code Playgroud)
但这不 -
print [].append(4)
Run Code Online (Sandbox Code Playgroud)
第二种情况的输出是None.你能解释一下输出吗?
我试图对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) 在我看来,我觉得list1.extend(list2)和list1.append(num)应该返回突变列表,而不是None(无效).除了具有破坏性地改变原始列表的副作用.
class FoodExpert:
def init(self):
self.goodFood = []
def addGoodFood(self, food):
self.goodFood.append(food)
def likes(self, x):
return x in self.goodFood
def prefers(self, x, y):
x_rating = self.goodFood.index(x)
y_rating = self.goodFood.index(y)
if x_rating > y_rating:
return y
else:
return x
Run Code Online (Sandbox Code Playgroud)
在声明这个类之后,我编写了这段代码:
>>> f = FoodExpert()
>>> f.init()
>>> map(f.addGoodFood, ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise'])
[None, None, None, None, None]
>>> f.goodFood
['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise']
Run Code Online (Sandbox Code Playgroud)
我无法理解地图功能如何在幕后工作,为什么它会返回一个包含所有的列表None,但是当我检查f.goodFood元素是否已添加到那里?
老实说,我只是不明白为什么会返回None而不是反向列表:
>>> l = range(10)
>>> print l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print l.reverse()
None
Run Code Online (Sandbox Code Playgroud)
为什么会这样?根据文件,我没有做错任何事.
我正在通过Udacity和Dave Evans介绍了关于列表属性的练习
list1 = [1,2,3,4]
list2 = [1,2,3,4]
list1=list1+[6]
print(list1)
list2.append(6)
print(list2)
list1 = [1,2,3,4]
list2 = [1,2,3,4]
def proc(mylist):
mylist = mylist + [6]
def proc2(mylist):
mylist.append(6)
# Can you explain the results given by the four print statements below? Remove
# the hashes # and run the code to check.
print (list1)
proc(list1)
print (list1)
print (list2)
proc2(list2)
print (list2)
Run Code Online (Sandbox Code Playgroud)
输出是
[1, 2, 3, 4, 6]
[1, 2, 3, 4, 6]
[1, 2, 3, 4]
[1, 2, 3, …Run Code Online (Sandbox Code Playgroud) 我试图根据我正在阅读的教程测试python中的列表是如何工作的.当我试图使用list.sort()或时list.reverse(),翻译给了我None.
请告诉我如何从这两种方法中获得结果:
a = [66.25, 333, 333, 1, 1234.5]
print(a.sort())
print(a.reverse())
Run Code Online (Sandbox Code Playgroud) 我正在开发一个程序的一部分,将陈述变成问题。
当我尝试删除它时x,它会返回None。我希望它打印删除该项目的句子,我做错了什么?
def Ask(Question):
Auxiliary = ("will", "might", "would", "do", "were", "are", "did")
for x in Auxiliary:
if x in Question:
Question_l = Question.lower()
Question_tk_l = word_tokenize(Question)
Aux_Rem = Question_tk_l.remove(x)
print (Aux_Rem)
Run Code Online (Sandbox Code Playgroud)
想要的行为示例:
"what we are doing in the woods"
Run Code Online (Sandbox Code Playgroud)
应该成为
"what we doing in the woods"
Run Code Online (Sandbox Code Playgroud)
我想从问题中删除任何辅助词。
如果您的问题作为此问题的重复项而被关闭,那是因为您有一些通用形式的代码
x = X()
# later...
x = x.y()
# or:
x.y().z()
Run Code Online (Sandbox Code Playgroud)
其中X是某种类型,它提供了y旨在z变异(修改)对象(X类型的实例)的方法。这可以适用于:
list、dict和setbytearray这种形式的代码很常见,但并不总是错误的。问题的明显迹象是:
与x.y().z()一样,会引发异常AttributeError: 'NoneType' object has no attribute 'z'。
有了x = x.y(),x就变成None, 而不是被修改的对象。这可能会被后来的错误结果发现,或者被像上面这样的异常(x.z()稍后尝试时)发现。
Stack Overflow 上有大量关于这个问题的现有问题,所有这些问题实际上都是同一个问题。之前甚至有多次尝试在特定上下文中涵盖同一问题的规范。然而,理解问题并不需要上下文,因此这里尝试一般性地回答:
代码有什么问题吗?为什么这些方法会这样,我们如何解决这个问题?
另请注意,当尝试使用 alambda(或列表理解)来产生副作用时,会出现类似的问题。
同样明显的问题可能是由因其他原因返回的方法引起的None …
当我试图找出我的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) python ×10
list ×6
nonetype ×2
sorting ×2
append ×1
arrays ×1
immutability ×1
python-3.x ×1
reverse ×1