Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的For和list_Python - Fatal编程技术网

Python中的For和list

Python中的For和list,python,Python,我对Python有一个问题。我有一个主要问题: def main(): cats = [] for x in range(1, 11): x = Cat(x) cats.append(x) listPussa = [] for x in range(1,21): x = Pussa(x) listPussa.append(x) PussaIndex = 0 for cat in

我对Python有一个问题。我有一个主要问题:

def main():
    cats = []
    for x in range(1, 11):
        x = Cat(x)
        cats.append(x)  


    listPussa = []
    for x in range(1,21):
       x = Pussa(x)
       listPussa.append(x)


    PussaIndex = 0
    for cat in cats:
        num = cat.getcatNum()

        if num % 2 != 0:
            for i in range(4):
                cat.addPussa(listPussa[PussaIndex])
                PussaIndex += 1

    for cat in cats:
      print cat.mixeta()
问题是,当我主要使用此功能时,要打印猫的列表:

def mixeta(self): #This is a method of the class Cat, it returns a few strings depending on how many objects Pussa has the object. 
    if len(self.__listPussa) is 0:  #When the cat has no one, it returns that
        return "Miau!", self.getcatNum(), "La llista de pusses esta buida" #The string menas that the list of Pussa is empty.

    else: #When it has at least 1
        listIdPussa = [] #I created a new list 
        for pussa in self.__listPussa: #And for every objcet Pussa in the list of Pussa
            listIdPussa.append(pussa.getIdPussa()) #I save every idPussa in the list.

        return  "Miau!", self.getcatNum(), "El gat esta infectat. El nombre total de pusses es ", len(self.__listPussa), listIdPussa #So, at the end it returns "Meow", the number of the cat, and it says that the cat is "infected" and the total number of objects Pussa is X and each id from every object
结果必须是:

('Miau!', 1, 'El gat esta infectat. El nombre total de pusses es ', 4[0,1,2,3])

('Miau!', 2, 'La llista esta buida') # It means the list has no objects Pussa

('Miau!', 3, 'El gat esta infectat. El nombre total de pusses es ', 4[4,5,6,7])

('Miau!', 4, 'La llista esta buida')
一直走到米奥,10号

但问题是我的主要印刷品是:

('Miau!', 1, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

('Miau!', 2, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

('Miau!', 3, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

('Miau!', 4, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

所有的猫都是这样。我能做什么?

这可能是因为您将
\uu listpusa
声明为类成员而不是对象成员。在这种情况下,它在
Cat
的所有实例之间共享

class Cat:
    __listPussa = []

    def __init__(self, num):
        self.num = num
每次向猫添加一些Pussa时,它们都会被添加到所有猫共享的同一变量中(yeek,猫共享它们的Pussa…:)

要实现您想要的,您需要将
\uu listpusa
设置为实例成员,如下所示:

class Cat:

    def __init__(self, num):
        self.num = num
        self.__listPussa = []

那是西班牙语的方言吗?还是加泰罗尼亚人?那它该怎么办?是的,它是加泰罗尼亚人!第一个结果是它应该做什么。因为你似乎已经从我的答案中提取了代码,你介意确保这个问题得到解决吗?@Atsuko不用担心,我们在某个时候都是新手,谢谢:)我也有这个<代码>代码def\uuuuuuu init\uuuuuuuuu(self,catNum=None,listPussa=[]):self.\uuuuuuucatnum=catNum self.\uuuuu listPussa=listPussa@Atsuko不幸的是,使用
[]
作为默认参数将不会真正起作用(请参阅)。在函数本身中将其设置为
[]
。谢谢!在类init中出现了错误!!真的非常感谢!