如何使用已创建的多个元素实现 class?
How to implement a class with multiple elements already created?
我正在尝试使用此结构实现 class:
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 = ["...","...","...","...","..","..."]
所以我试图找到一种使用 class 来实现它的 pythonic 方法,并避免创建一堆对象或一堆列表来存储我拥有的所有类型的组合.
像列表一样管理它并按位置访问其元素而不是创建 class 会更好吗?
您认为我可以做些什么来改进我的代码?
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。 :/
谢谢:)
假设这些列表是文本字符串,您可以只解析该列表并放入 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 调用你 class 构造函数。
根据您的编辑,您可以:
>>> 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'}
我正在尝试使用此结构实现 class:
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 = ["...","...","...","...","..","..."]
所以我试图找到一种使用 class 来实现它的 pythonic 方法,并避免创建一堆对象或一堆列表来存储我拥有的所有类型的组合.
像列表一样管理它并按位置访问其元素而不是创建 class 会更好吗? 您认为我可以做些什么来改进我的代码?
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。 :/
谢谢:)
假设这些列表是文本字符串,您可以只解析该列表并放入 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 调用你 class 构造函数。
根据您的编辑,您可以:
>>> 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'}