如何将列表传递给__init__值/定义? [英] How do I pass a list to a __init__ value/definition?

查看:121
本文介绍了如何将列表传递给__init__值/定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我先从我的免责声明开始,说我是新来的电脑

编程并在过去三周内完成了这项工作。我可能不会用所有的行话来完全正确,所以请耐心等待。


无论如何,我正在编写一个有类的函数名为

MultipleRegression。我希望__init__

方法下的一个变量成为一个列表。我有:


class MultipleRegression:

def __init __(self,dbh,regressors,fund):

self .dbh = dbh

self.regressors = regressors


我希望能够输入回归量列表如

MultipleRegression(dbh,[1,2,3,4],5)。但是当我这样做时,只有1

被传递给回归量,从而传递给self.regressors。有没有

简单的方法来解决这个问题?请记住,列表的长度可能会有所不同,所以我不能只创建一组变量,然后将它们混合成一个列表。


非常感谢!我真的很喜欢这个编程的东西。

它真正具有挑战性,对我的工作非常有用。

解决方案

< ry *********** @ gmail.comwrote in message

news:11 **************** *****@h48g2000cwc.googlegro ups.com ...


class MultipleRegression:

def __init __(self,dbh,regressors ,基金):

self.dbh = dbh

self.regressors = regressors


我希望能够进入回归量列表如

MultipleRegression(dbh,[1,2,3,4],5)。但是当我这样做时,只有1

被传递给回归量,从而传递给self.regressors。



真的吗?


class MultipleRegression:

def __init __(self,dbh,regressors,基金):

self.dbh = dbh

self.regressors = regressors

foo = MultipleRegression(42,[1,2,3, 4],5)

打印foo.regressors


打印[1,2,3,4]


试一试,看看。


ry *** ********@gmail.com 写道:


我有:


class MultipleRegression:

def __init __(self,dbh,regressors,fund):

self.dbh = dbh

self.regressors =回归量


我希望能够输入回归量列表如

MultipleRegression(dbh,[1,2,3,4],5) 。但是当我这样做时,只有1

被传递给回归量,从而传递给self.regressors。



你的问题出在其他地方,因为当我这样做时,我得到了

正确的结果:


>> mr = MultipleRegression(10,[1,2,3,4],5)
print mr.regressors



[1,2,3,4]

所以我想的方式你把你的清单传给MultipleRegression是

也许是错的。我希望你的问题是在

中从未知数量的项目中创建该列表。基本上你需要

创建列表,重复添加必要的项目,直到你完成
,然后将它传递给MultipleRegression。如何创建和

填充该列表将取决于您从何处获取数据。


-

Ben Sizer


2006年7月25日05:46:55 -0700, ry *********** @ gmail.com

< ry *********** @ gmail.comwrote:


让我先从我的免责声明开始,说我是电脑新手

编程并且已经做了过去三周。我可能不会用所有的行话来完全正确,所以请耐心等待。


无论如何,我正在编写一个有类的函数名为

MultipleRegression。我希望__init__

方法下的一个变量成为一个列表。我有:


class MultipleRegression:

def __init __(self,dbh,regressors,fund):

self .dbh = dbh

self.regressors = regressors


我希望能够输入回归量列表如

MultipleRegression(dbh,[1,2,3,4],5)。但是当我这样做时,只有1

被传递给回归量,从而传递给self.regressors。有没有

简单的方法来解决这个问题?请记住,列表的长度可能会有所不同,所以我不能只创建一组变量,然后将它们混合成一个列表。



你的工作对我来说很好:


Python 2.4.1(#2,2005年3月31日,00: 05:10)

[GCC 3.3 20030304(Apple Computer,Inc。build 1666)]在darwin上

输入help,copyright,credit ;或许可证或欲获得更多信息。


>> class MultipleRegression:



.... def __init __(self,dbh,regressors,fund):

.... self.dbh = dbh

.... self.regressors = regressors

....


>> spam = MultipleRegression(''dbh'',[1,2,3,4],5)
spam.regressors



[1,2,3,4]


是什么让你认为你只有列表的第一个成员?可以

你告诉我们那些'不工作的代码吗?


-

干杯,

Simon B,
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/


Let me start with my disclaimer by saying I''m new to computer
programming and have doing it for the past three weeks. I may not be
completely correct with all the jargon, so please bear with me.

Anyways, I''m writing a function which has a class called
"MultipleRegression." I want one of the variables under the __init__
method to be a list. I''ve got:

class MultipleRegression:
def __init__(self, dbh, regressors, fund):
self.dbh = dbh
self.regressors = regressors

and I want to be able to enter regressors as a list like
MultipleRegression(dbh, [1,2,3,4], 5). But when I do this only the 1
gets passed to regressors and thus to self.regressors. Is there any
simple way to fix this? Keep in mind that the length of the list may
vary, so I can''t just create a set number of variables and then mash
them together into a list.

Thanks so much! I really am getting into this whole programming thing.
Its real challenging and very useful for my work.

解决方案

<ry***********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...

class MultipleRegression:
def __init__(self, dbh, regressors, fund):
self.dbh = dbh
self.regressors = regressors

and I want to be able to enter regressors as a list like
MultipleRegression(dbh, [1,2,3,4], 5). But when I do this only the 1
gets passed to regressors and thus to self.regressors.

Really?

class MultipleRegression:
def __init__(self, dbh, regressors, fund):
self.dbh = dbh
self.regressors = regressors
foo = MultipleRegression(42, [1,2,3,4], 5)
print foo.regressors

prints [1,2,3,4]

Try it and see.


ry***********@gmail.com wrote:

I''ve got:

class MultipleRegression:
def __init__(self, dbh, regressors, fund):
self.dbh = dbh
self.regressors = regressors

and I want to be able to enter regressors as a list like
MultipleRegression(dbh, [1,2,3,4], 5). But when I do this only the 1
gets passed to regressors and thus to self.regressors.

Your problem lies elsewhere, as when I do exactly that, I get the
correct results:

>>mr = MultipleRegression(10, [1,2,3,4], 5)
print mr.regressors

[1, 2, 3, 4]
So I think the way you are passing your list to MultipleRegression is
perhaps wrong. I expect your problem is in creating that list in the
first place from an unknown number of items. Basically you need to
create the list, repeatedly add the necessary items to it until you''re
done, and then pass that to MultipleRegression. How to create and
populate that list will depend on where you''re getting the data from.

--
Ben Sizer


On 25 Jul 2006 05:46:55 -0700, ry***********@gmail.com
<ry***********@gmail.comwrote:

Let me start with my disclaimer by saying I''m new to computer
programming and have doing it for the past three weeks. I may not be
completely correct with all the jargon, so please bear with me.

Anyways, I''m writing a function which has a class called
"MultipleRegression." I want one of the variables under the __init__
method to be a list. I''ve got:

class MultipleRegression:
def __init__(self, dbh, regressors, fund):
self.dbh = dbh
self.regressors = regressors

and I want to be able to enter regressors as a list like
MultipleRegression(dbh, [1,2,3,4], 5). But when I do this only the 1
gets passed to regressors and thus to self.regressors. Is there any
simple way to fix this? Keep in mind that the length of the list may
vary, so I can''t just create a set number of variables and then mash
them together into a list.

What you have works fine for me:

Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>class MultipleRegression:

.... def __init__(self, dbh, regressors, fund):
.... self.dbh = dbh
.... self.regressors = regressors
....

>>spam = MultipleRegression(''dbh'', [1,2,3,4], 5)
spam.regressors

[1, 2, 3, 4]

What makes you think you only have the first member of the list? Can
you show us the code that''s not working?

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/


这篇关于如何将列表传递给__init__值/定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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