向课程添加列表? [英] Add lists to class?

查看:71
本文介绍了向课程添加列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中包含一些字符串,''一个',''两个''''三'',所以

on。我想将列表添加到具有这些名称的类中。我没有

的方式知道列表中的内容或列表将在多长时间内提前

.


类似的东西:


班主:

def __init __(自我,列表):

self.count = len(list)

列表中的行:

self.line = []#显然这不起作用


list = [ ''one'',''two'',''three'']


a =主人(清单)


所以我得到:


dir(a)


[''_doc__'',''_ init__'',''_ module _'',' '一'',''两'',''三'']


而不是:


dir(a)


[''_doc__'',''_init__'',''_ module__'',''line'']

TIA,


jab

解决方案

感谢慷慨的Pyhtonista回复了指向setattr()的指针。


jab


我想你想要的是这个(未经测试):


班主:

def __init __(self,list):

self.count = len(list)

列表中的条目:

self .__ dict__ [entry] = []

这将使你获得具有三个属性的大师级


a = master(list)


a.one

a.two

a.three


每个包含一个空列表。你可以做一些事情

喜欢:


a.one.append(某事物)

a.three.append(某物) )


拉里贝茨


BBands写道:

我有一个包含一些字符串的列表,' 一个,两个,三个等等。我想将列表添加到具有这些名称的类中。我没有知道列表中的内容或列表将在多长时间内提前的方式。

类似于:

类大师:
def __init __(自我,列表):
self.count = len(list)
列表中的行:
self.line = []#显然这不是'工作

list = [''one'',''two'',''three'']

a = master(list)

所以我得到:

dir(a)

[''_doc__'',''_init__'',''_ module _'',''一个'',''''',''三个'']

代替:

dir(a)

['' _doc__'',''_init__'',''_ _ module__'',''line'']

TIA,

jab



测试和工作......


jab,现在拥有财富的尴尬,说:谢谢!


I have a list with some strings in in it, ''one'', ''two'' ''three'' and so
on. I would like to add lists to a class with those names. I have no
way of knowing what will be in the list or how long the list will be in
advance.

Something like:

class master:
def __init__(self, list):
self.count = len(list)
for line in list:
self.line = [] # obviously this doesn''t work

list = [''one'', ''two'', ''three'']

a = master(list)

so that I get:

dir(a)

[''_doc__'', ''_init__'', ''_module__'', ''one'', ''two'', ''three'']

instead of:

dir(a)

[''_doc__'', ''_init__'', ''_module__'', ''line'']
TIA,

jab

解决方案

Thanks to a generous Pyhtonista replied with a pointer to setattr().

jab


I think what you want is this (not tested):

class master:
def __init__(self, list):
self.count = len(list)
for entry in list:
self.__dict__[entry]= []
This will get you master class with three attributes

a = master(list)

a.one
a.two
a.three

each containing an empty list. You can then do things
like:

a.one.append(something)
a.three.append(something)

Larry Bates

BBands wrote:

I have a list with some strings in in it, ''one'', ''two'' ''three'' and so
on. I would like to add lists to a class with those names. I have no
way of knowing what will be in the list or how long the list will be in
advance.

Something like:

class master:
def __init__(self, list):
self.count = len(list)
for line in list:
self.line = [] # obviously this doesn''t work

list = [''one'', ''two'', ''three'']

a = master(list)

so that I get:

dir(a)

[''_doc__'', ''_init__'', ''_module__'', ''one'', ''two'', ''three'']

instead of:

dir(a)

[''_doc__'', ''_init__'', ''_module__'', ''line'']
TIA,

jab



tested and working...

jab, now possessing an embarrassment of riches, says "Thanks!"


这篇关于向课程添加列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆