Python. How to efficiently remove custom object from array

egorikem

Good afternoon dear colleagues! I have maybe quite an obvious question, but it can be considered as quite a mystery to me.

So, I have two custom classes, let it be Class A and Class B. Class B has a property, which contains multiple instances of Class A. It also contains methods to work with this property: add, remove, get single and all instances in this property.

However, apart from standard and quite over questioned deal with MVC pattern, I wonder: what is the most efficient and fast method to remove an object from this property (array) in Python, which implements some customization (e.g. compare objects by id, title and other properties).

I had implemented my own, but it seems way too complicated for such an essential operation.

class Skill:

    def __init__(self, id_):
        self.id = id_

class Unit:

    def __init__(self):
        self.skills = []

    def get_skills(self):
        return self.skills

    def get_skill(self, index):
        return self.skills[index]

    def add_skill(self, skill):
        self.skills.append(skill)

    def remove_skill(self, skill_to_remove):
        self.skills = filter(lambda skill: skill.id != skill_to_remove.id, self.skills)
DNA

If you need arbitrary criteria, then filtering is OK, but it is slightly shorter to use a list comprehension. For example, instead of

self.skills = filter(lambda skill: skill.id != skill_to_remove.id, self.skills)

use:

self.skills = [s for s in self.skills if s.id != skill_to_remove.id]

It's also possible to modify the list in-place (see this question) using slice assignment:

self.skills[:] = (s for s in self.skills if s.id != skill_to_remove.id)

If you are filtering skills based on an exact match with a "template" skill, i.e. matching all the properties of skill_to_remove then it might be better to implement an equality method for your Skill class (see this question). Then you could just use the remove method on self.skills. However, this will only remove the first matching instance.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Remove object from array

분류에서Dev

Remove object from JSON array

분류에서Dev

Javascript: Remove object from array on function call to object

분류에서Dev

How to remove object with specific name from ArrayList

분류에서Dev

How to remove single object from the sessionStorage (AngularJs)

분류에서Dev

How to update view from custom Timer object

분류에서Dev

how to remove file details from multi array

분류에서Dev

How do I remove values from an array?

분류에서Dev

How to remove \0 from the array of strings

분류에서Dev

Check if bullet object has left the screen and remove from array

분류에서Dev

How to remove a row from the javascript object based on a field and realign the object

분류에서Dev

How to get object from array in swift

분류에서Dev

how is Array object different from other objects

분류에서Dev

How to remove HTML tags from python strings?

분류에서Dev

How to remove duplicate entries from a list in python

분류에서Dev

How to remove a property from a JavaScript object when using Strict Mode

분류에서Dev

How to return an array of object properties from a returned object of Laravel Eloquent

분류에서Dev

How to remove all next elements from array in javascript or lodash

분류에서Dev

How to remove duplicate values from a multidimentional array according to a key

분류에서Dev

How do I give a string object a custom method in Python?

분류에서Dev

Javascript - count and remove from an object

분류에서Dev

NSUserDefault Custom object error - "array cannot be bridged from Objective-C"

분류에서Dev

NSUserDefault Custom object error - "array cannot be bridged from Objective-C"

분류에서Dev

How to remove 0s from dictionary in python

분류에서Dev

How do I remove a character once from a string [python]?

분류에서Dev

Remove array key from multidimensional array

분류에서Dev

How to get array object in object

분류에서Dev

How can I do an array of object of my class (with inheritance of another custom class)?

분류에서Dev

How to remove whitespace in array element?

Related 관련 기사

  1. 1

    Remove object from array

  2. 2

    Remove object from JSON array

  3. 3

    Javascript: Remove object from array on function call to object

  4. 4

    How to remove object with specific name from ArrayList

  5. 5

    How to remove single object from the sessionStorage (AngularJs)

  6. 6

    How to update view from custom Timer object

  7. 7

    how to remove file details from multi array

  8. 8

    How do I remove values from an array?

  9. 9

    How to remove \0 from the array of strings

  10. 10

    Check if bullet object has left the screen and remove from array

  11. 11

    How to remove a row from the javascript object based on a field and realign the object

  12. 12

    How to get object from array in swift

  13. 13

    how is Array object different from other objects

  14. 14

    How to remove HTML tags from python strings?

  15. 15

    How to remove duplicate entries from a list in python

  16. 16

    How to remove a property from a JavaScript object when using Strict Mode

  17. 17

    How to return an array of object properties from a returned object of Laravel Eloquent

  18. 18

    How to remove all next elements from array in javascript or lodash

  19. 19

    How to remove duplicate values from a multidimentional array according to a key

  20. 20

    How do I give a string object a custom method in Python?

  21. 21

    Javascript - count and remove from an object

  22. 22

    NSUserDefault Custom object error - "array cannot be bridged from Objective-C"

  23. 23

    NSUserDefault Custom object error - "array cannot be bridged from Objective-C"

  24. 24

    How to remove 0s from dictionary in python

  25. 25

    How do I remove a character once from a string [python]?

  26. 26

    Remove array key from multidimensional array

  27. 27

    How to get array object in object

  28. 28

    How can I do an array of object of my class (with inheritance of another custom class)?

  29. 29

    How to remove whitespace in array element?

뜨겁다태그

보관