问题
guys! So I recently started learning about python classes and objects. For instance, I have a following list of strings:
alist = ["Four", "Three", "Five", "One", "Two"]
Which is comparable to a class of Numbers I have:
class Numbers(object):
One=1
Two=2
Three=3
Four=4
Five=5
How could I convert alist
into
alist = [4, 3, 5, 1, 2]
based on the class above?
My initial thought was to create a new (empty) list and use a for loop
that adds the corresponding object value (e.g. Numbers.One
) to the empty list as it goes through alist
. But I'm unsure whether that'd be the most efficient solution.
Therefore, I was wondering if there was a simpler way of completing this task using Python Classes / Inheritance.
I hope someone can help me and explain to me what way would work better and why!
Thank you!!
回答1:
While I totally agree that using a dict
for Numbers
would be easier and straight forward, but showing you the Enum
way as your class involves magic numbers and sort of a valid use case for using enums.
A similar implementation using Enum
would be:
from enum import Enum
class Numbers(Enum):
One = 1
Two = 2
Three = 3
Four = 4
Five = 5
Then you can use getattr
and Numbers.<attr>.value
to get the constant numbers:
In [592]: alist = ["Four", "Three", "Five", "One", "Two"]
In [593]: [getattr(Numbers, n).value for n in alist]
Out[593]: [4, 3, 5, 1, 2]
Edit based on comment:
If you want to get the names back from a number list:
In [952]: l = [4, 3, 5, 1, 2]
In [953]: [Numbers(num).name for num in l]
Out[953]: ['Four', 'Three', 'Five', 'One', 'Two']
回答2:
If you are set on using the class, one way would be to use __getattribute__()
print([Numbers().__getattribute__(a) for a in alist])
#[4, 3, 5, 1, 2]
But a much better (and more pythonic IMO) way would be to use a dict
:
NumbersDict = dict(
One=1,
Two=2,
Three=3,
Four=4,
Five=5
)
print([NumbersDict[a] for a in alist])
#[4, 3, 5, 1, 2]
回答3:
Most objects (and hence classes) in python have the __dict__
field, which is a mapping from attribute names to their values. You can access this field using the built-in vars, so
values = [vars(Numbers)[a] for a in alist]
will give you what you want.
回答4:
EDIT: I suppose that the words and numbers are just a trivial example, a dictionary is the right way to do it if that's not the case as written in the comments.
Your assumptions are correct - either create an empty list and populate it using for loop, or use list comprehension with a for loop to create a new list with the required elements.
Empty list with for loop
#... Numbers class defined above
alist = ["Four", "Three", "Five", "One", "Two"]
nlist = []
numbers = Numbers()
for anumber in alist:
nlist.append(getattr(numbers, anumber))
print(nlist)
[4, 3, 5, 1, 2]
List comprehension with for loop
#... Numbers class defined above
alist = ["Four", "Three", "Five", "One", "Two"]
numbers = Numbers()
nlist = [getattr(numbers, anumber) for anumber in alist]
print(nlist)
[4, 3, 5, 1, 2]
来源:https://stackoverflow.com/questions/56794886/how-to-convert-lists-of-class-objects-to-a-list-of-their-attributes