Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
60 views
in Technique[技术] by (71.8m points)

python - delete 0 value in list

enter image description herehello I try to make an app- personality test, but i have a problem with evaluation,.. there is a dificult evaluation of test but in short in theese reduce version you just write some values into a given variable, and i want to delete every 0 value from list as a result.. I tried it (programe after # but there is an error), could anybody please help me?

error

 File "C:/Users/Stanko/PycharmProjects/pythonProjectDISK/main.py", line 73, in dele
    self._data[self.item.key]= list(filter(lambda num: num != 0, self._data[self.item.key]))
AttributeError: 'Test' object has no attribute 'item'

here is a reduce program



class Empty(Exception):
    pass


class PriorityVal(ABC):
    class _Item:
        def __init__(self, key, value):
            self.key, self.value = key, value

        def __lt__(self, other):
            return self.key < other.key

        def __repr__(self):
            return str((self.key, self.value))

    @abstractmethod
    def add(self, key, value):
        pass

    @abstractmethod
    def max(self):
        pass

    @abstractmethod
    def min(self):
        pass

    @abstractmethod
    def remove_min(self):
        pass


class Test(PriorityVal):
    def __init__(self):
        self._data = []

    def add(self, key, value):
        self._data.append(self._Item(key, value))

    def max(self):
        index = self.find_max()
        item = self._data[index]
        return item.key, item.value

    def find_max(self):
        index = 0
        for i in range(1, len(self._data)):
            if self._data[index] < self._data[i]:
                index = i
        return index

    def _find_min(self):
        index = 0
        for i in range(1, len(self._data)):
            if self._data[i] < self._data[index]:
                index = i
        return index

    def min(self):
        index = self._find_min()
        item = self._data[index]
        return item.key, item.value

    def remove_min(self):
        index = self._find_min()
        item = self._data.pop(index)
        return item.key, item.value

    def dele(self):
        self._data[self.item.key]= list(filter(lambda num: num != 0, self._data[self.item.key]))


test = str(input("test name"))
test = Test()

dzadane = int(input("input d"))
if dzadane <= 0:
    dh = 0
elif 0 < dzadane <= 4:
    dh = 60
elif 4 < dzadane <= 7:
    dh = 70
elif 7 < dzadane <= 12:
    dh = 80
elif 12 < dzadane <= 15:
    dh = 90
else:
    dh = 100
test.add(dh, "d")

izadane = int(input("input i"))
if izadane <= -1:
    ih = 0
elif -1 < izadane <= 1:
    ih = 60
elif 1 < izadane <= 3:
    ih = 70
elif izadane == 4:
    ih = 75
elif 4 < izadane <= 6:
    ih = 80
elif izadane == 7:
    ih = 85
elif 7 < izadane <= 9:
    ih = 90
else:
    dh = 100
test.add(ih, "i")

szadane = int(input("input s"))
if szadane <= -2:
    sh = 0
elif -2 < szadane <= 0:
    sh = 60
elif 0 < szadane <= 3:
    sh = 70
elif 4 < szadane <= 7:
    sh = 80
elif szadane == 8:
    sh = 85
elif 8 < szadane <= 10:
    sh = 90
else:
    sh = 100
test.add(sh, "s")

kzadane = int(input("input k"))
if kzadane <= -3:
    kh = 0
elif -3 < kzadane <= 1:
    kh = 60
elif -1 < kzadane <= 1:
    kh = 70
elif 1 < kzadane <= 4:
    kh = 80
elif 4 < kzadane <= 7:
    kh = 90
else:
    kh = 100
test.add(kh, "k")

nzadane = int(input("input n"))
nh = 0
test.add(nh, "n")

print(dzadane, izadane, szadane, kzadane, nzadane, )
print("test", test._data)
print('max=', test.max())

print( "del all 0", test.dele())
print("test", test._data)
question from:https://stackoverflow.com/questions/65847902/delete-0-value-in-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could try

self._data = [value for value in self._data if value.key != 0]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...