There is a list of lists
[[name1, value1], [name2, value2], ...]
I need to create instances of a class with names name1, name2, and so forth, ie, with names taken from the list[1][1]
, list[2][1]
, etc. But I can't imagine ways in which this can be implemented.
Class:
class func():
def __init__(self, visibility, ftype, body):
...
List:
list = [
['private', 'Void', 'SetupWheels', 'body'],
...
]
Dictionary:
func_list = {}
It should look like this:
for i, val in enumerate(c):
*new key in the dictionary is equal to the value val[2]* = func(val[0], val[1], val[3])
To populate a dictionary with instances of a class whose attributes were taken from a list of lists, you can use a dict
comprehension like:
func_list = {row[2]: Func(row[0], row[1], row[3]) for row in c}
class Func():
def __init__(self, visibility, ftype, body):
self.visibility = visibility
self.ftype = ftype
self.body = body
def __repr__(self):
return "v:%s f:%s b:%s" % (self.visibility, self.ftype, self.body)
c = [
['private', 'Void', 'SetupWheels', 'body'],
['private', 'Void', 'SetupWheelx', 'bo8y'],
]
func_list = {row[2]: Func(row[0], row[1], row[3]) for row in c}
print(func_list)
{'SetupWheelx': v: private f:Void b:body, 'SetupWheels': v: private f:Void b:body}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.