相关疑难解决方法(0)

为什么[] .append()在python中不起作用?

为什么这样做 -

a = []
a.append(4)
print a
Run Code Online (Sandbox Code Playgroud)

但这不 -

print [].append(4)
Run Code Online (Sandbox Code Playgroud)

第二种情况的输出是None.你能解释一下输出吗?

python

9
推荐指数
1
解决办法
3878
查看次数

为什么'.sort()'导致Python中的列表为'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)

python sorting list

8
推荐指数
3
解决办法
1万
查看次数

Python - 为什么extend()和append()返回None(void)?

可能重复:
为什么python的list.append评估为false?

在我看来,我觉得list1.extend(list2)list1.append(num)应该返回突变列表,而不是None(无效).除了具有破坏性地改变原始列表的副作用.

python arrays immutability

8
推荐指数
1
解决办法
9161
查看次数

python中的map方法

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元素是否已添加到那里?

python functional-programming list

8
推荐指数
2
解决办法
1万
查看次数

list.reverse()不起作用

老实说,我只是不明白为什么会返回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)

为什么会这样?根据文件,我没有做错任何事.

python reverse list

8
推荐指数
3
解决办法
8164
查看次数

为什么+运算符不会在.append()的情况下更改列表?

我正在通过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 concatenation append

7
推荐指数
1
解决办法
1650
查看次数

sort()和reverse()函数不起作用

我试图根据我正在阅读的教程测试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)

python sorting list

7
推荐指数
1
解决办法
5665
查看次数

当我尝试从列表中删除变量时,remove() 返回 None

我正在开发一个程序的一部分,将陈述变成问题。

当我尝试删除它时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)

我想从问题中删除任何辅助词。

python python-3.x

7
推荐指数
1
解决办法
2万
查看次数

在`x = xy()`之后,为什么`x`变成了`None`而不是被修改(可能导致“AttributeError:'NoneType'对象没有属性”)?

如果您的问题作为此问题的重复项而被关闭,那是因为您有一些通用形式的代码

x = X()
# later...
x = x.y()
# or:
x.y().z()
Run Code Online (Sandbox Code Playgroud)

其中X是某种类型,它提供了y旨在z变异修改)对象(X类型的实例)的方法。这可以适用于:

  • 可变的内置类型,例如listdictsetbytearray
  • 标准库(尤其是 Tkinter 小部件)或第三方库提供的类。

这种形式的代码很常见,但并不总是错误的。问题的明显迹象是:

  • x.y().z()一样,会引发异常AttributeError: 'NoneType' object has no attribute 'z'

  • 有了x = x.y(),x就变成None, 而不是被修改的对象。这可能会被后来的错误结果发现,或者被像上面这样的异常(x.z()稍后尝试时)发现。

Stack Overflow 上有大量关于这个问题的现有问题,所有这些问题实际上都是同一个问题。之前甚至有多次尝试在特定上下文中涵盖同一问题的规范。然而,理解问题并不需要上下文,因此这里尝试一般性地回答:

代码有什么问题吗?为什么这些方法会这样,我们如何解决这个问题?


另请注意,当尝试使用 alambda或列表理解)来产生副作用时,会出现类似的问题。

同样明显的问题可能是由因其他原因返回的方法引起的None …

python attributeerror command-query-separation nonetype

7
推荐指数
1
解决办法
577
查看次数

无法对我的列表进行排序,因为它是NoneType?简单的Python

当我试图找出我的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 list nonetype

6
推荐指数
2
解决办法
1万
查看次数