I currently have a list of lists where every list consists of the same kind of information, say:
[['Planet Name', 16, 19, 27, 11], ['Planet Name 2', 12, 22, 11, 42], ....]
。
。
我真的很感激一些帮助,尝试学习Python和当前的类!
最佳答案
好。。。要使一个类成为您想要的类,您可以执行以下操作:
class Planet(object):
def __init__(self, *args, **kwargs):
self.name = args[0]
self.distance = args[1]
# ... etc ...
或者像这样的:
class Planet(object):
def __init__(self, name, distance, ...):
self.name = name
self.distance = distance
# ... etc ...
然后你这样称呼它:
p = Planet(*['Planet Name', 16, 19, 27, 11])
在这样一个循环中:
l = [['Planet Name', 16, 19, 27, 11], ['Planet Name 2', 12, 22, 11, 42], ....]
planets = [Planet(*data) for data in l]
关于python - 将列表列表变成对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20066308/