我试图用这种结构实现一个类:
class type_of_thing():
def __init__(self, type, code, version, description, year, family):
self.type = type
self.code = code
self.version = version
self.description = description
self.year = year
self.family = family
# with this I would create a new element of this class:
obj_type1 = type_of_thing(type = "x", code = "WRT-001", version= "1-10", description= "custom", year = "2016", family = "class1")
obj_type1_2 = type_of_thing(type = "x", code = "WRT-001", version= "1-11", description= "custom", year = "2016", family = "class1")
obj_type2 = type_of_thing(type = "xy", code = "WRT-001", version= "1-12", description= "custom", year = "2016", family = "class1")
但我必须创建数百个这样的元素,我已经将它们放在这样的列表中:
type of thing1:
type = "x",
code = "WRT-001",
version= "1-10",
description= "custom",
year = "2016",
family = "class1"
type = "x",
code = "WRT-001",
version= "1-11",
description= "custom",
year = "2016",
family = "class1"
type of thing2:
type = "xy",
code = "WRT-001",
version= "1-12",
description= "custom",
year = "2016",
family = "class1"
type = "xy",
code = "WRT-001",
version= "1-10",
description= "custom",
year = "2016",
family = "class1"
type of thing3:
...
...
...
让我们说我可以将它们放在这样的列表中:
type_of_thing1 = ["x","WRT-001","1-10","custom","2016","class1"]
type_of_thing1_2 = ["x","WRT-001","1-11","custom","2016","class1"]
type_of_thing2 = ["xy","WRT-001","1-12","custom","2016","class1"]
type_of_thing3 = ["...","...","...","...","..","..."]
所以我试图找到一种pythonic方法来使用类来实现它,并避免创建一堆对象或一堆列表来存储我拥有的所有类型的组合。
像列表一样管理它,按位置访问其元素而不是创建类会更好吗? 您认为我可以做些什么来改进我的代码?
答案 0 :(得分:0)
假设这些列表是文本字符串,您只需解析该列表并放入dict
即可。这是一个例子:
txt='''\
type = "x",
code = "WRT-001",
version= "1-10",
description= "custom",
year = "2016",
family = "class1"
type = "x",
code = "WRT-001",
version= "1-11",
description= "custom",
year = "2016",
family = "class1"'''
for block in txt.split('\n\n'):
di={}
for line in block.splitlines():
k,v=[e.strip().strip('"') for e in line.rstrip().rstrip(',').split('=')]
di[k]=v
print(di)
打印:
{'type': 'x', 'code': 'WRT-001', 'version': '1-10', 'description': 'custom', 'year': '2016', 'family': 'class1'}
{'type': 'x', 'code': 'WRT-001', 'version': '1-11', 'description': 'custom', 'year': '2016', 'family': 'class1'}
然后使用dict di调用类构造函数。
根据您的修改,您可以执行以下操作:
>>> keys=['type', 'code', 'version', 'description', 'year', 'family']
>>> vals=["x","WRT-001","1-10","custom","2016","class1"]
>>> {k:v for k,v in zip(keys, vals)}
{'type': 'x', 'code': 'WRT-001', 'version': '1-10', 'description': 'custom', 'year': '2016', 'family': 'class1'}
答案 1 :(得分:-1)
Python将它的全局变量存储在globals()
中,您还提到了一个信息列表,您可以遍历该列表,将类添加到globals()
。
list_of_things = [("x","WRT-001","1-10","custom","2016","class1"),
("x","WRT-001","1-11","custom","2016","class1"),
("xy","WRT-001","1-12","custom","2016","class1")]
class type_of_thing():
def __init__(self, type, code, version, description, year, family):
self.type = type
self.code = code
self.version = version
self.description = description
self.year = year
self.family = family
for i,thing in enumerate(list_of_things):
globals()["thing_"+str(i)] = type_of_thing(thing[0],thing[1],thing[2],thing[3],thing[4],thing[5])
print(thing_0)
此代码将创建变量名thing_0
至thing-<Number of list indicies>
,然后打印第一个thing_0
。
希望这有帮助!如果它不是你想要的,请告诉我。
另外它不是非常pythonic。 :/
谢谢:)