Python List附加搞砸了

时间:2015-01-31 23:32:30

标签: python list

这个程序有什么问题? 当我编译它时,其中一个名为“single_bovines”的列表会被追加,即使没有命令告诉任何要附加到它的内容。 以#debug行结尾的行是我尝试调试程序的行。

class Cow(object):
        def __init__(self, height, weight, strength):
                self.height = height
        self.weight = weight
        self.strength = strength
class Combination(object):
    def __init__(self, cows):
        self.cows = cows
        self.evaluated_flag = False
        self.satisfied_height = False
        self.satisfied_strength = False
        self.safety_factor = None

    def evaluate(self, mark_height):
        if sum(map(lambda c: c.height, self.cows)) >= mark_height:
            self.satisfied_height = True

        self.satisfied_strength = True
        for i, cow in enumerate(self.cows):
            if sum(map(lambda c: c.weight, self.cows[i+1:])) > cow.strength:
                self.satisfied_strength = False
                break

        if self.satisfied_height and self.satisfied_strength:
            self.safety_factor = 999999
            for i, cow in enumerate(self.cows):
                self.safety_factor = min(cow.strength - sum(map(lambda c: c.weight, self.cows[i+1:])), self.safety_factor)

        return self.satisfied_height, self.satisfied_strength

    def get_safety_factor(self):
        return self.safety_factor


file = open('guardmark.in', 'r').readlines()
number_of_cows, mark_height = map(lambda item: int(item), file[0].rstrip('\n').split(' '))
consideration_list = []
for line in file[1:]:
    height, weight, strength = map(lambda item: int(item), line.rstrip('\n').split(' '))
    consideration_list.append(Combination([Cow(height, weight, strength)]))
single_bovines = consideration_list
complete_list = []
consideration_done = True

while filter(lambda c: c.evaluated_flag == False, consideration_list):
    for combo in consideration_list:
        print "a combo" #debug line
        height_flag, strength_flag = combo.evaluate(mark_height)
        if height_flag and strength_flag:
            combo.evaluated_flag = True
            complete_list.append(combo)
        if not strength_flag:
            combo.evaluated_flag = True
        if strength_flag and not height_flag:
            combo.evaluated_flag = True
            print "put into consideration" #debug line
            for single_cow in single_bovines:
                print "a cow" , len(single_bovines)#debug line
                if single_cow not in combo.cows:
                    combo_cows_copy = combo.cows
                    combo_cows_copy.append(single_cow)
                    consideration_list.append(Combination(combo_cows_copy))
if not complete_list:
    print "Mark is too tall"
else:
    safety_factors = []
    for combo in complete_list:
        safety_factors.append(combo.get_safety_factor())
    print max(safety_factors)

2 个答案:

答案 0 :(得分:5)

我认为问题可能是你做的

single_bovines = consideration_list

...这意味着两个变量名称都指向同一个列表。如果您想要两个不同的列表(两者都具有相同的内容),请使用以下任何一个:

single_bovines = list(consideration_list)
single_bovines = consideration_list.copy()
single_bovines = consideration_list[:]

所有这三个人都会将原始列表的内容复制到一个新列表中。

答案 1 :(得分:0)

请参阅此处的答案How to clone or copy a list?我怀疑,由于您没有为Conside_list制作一份正确的副本,因此会附加到single_bovines。