简体   繁体   English

将参数集列表传递给函数

[英]Passing a list of argument sets to a function

I've made a little progress on my understanding since submitting the poorly posed question below. 自从提交下面提出的不良问题以来,我在理解上取得了一些进展。 I've gotten as far as this: 我已经达到了这个目的:

from collections import namedtuple
a = 3
Data = namedtuple('Data', 'b c')
d1 = Data(4,5)
d2 = Data(6,7)
data_list = (d1, d2)
def test1(a, *data) :
...

But *data is clearly not the correct syntax I seek for passing data_list. 但*数据显然不是我寻求传递data_list的正确语法。 How do I structure this so the function knows that this is a namedtuple list, defined as Data. 我如何构造它,以便函数知道这是一个名为数据的名单元素列表。

In the function, I need to do something like: print 'A = ', a for each in data print 'BC = ', data.b + data.c 在函数中,我需要执行以下操作:print'A =',a中的每个数据打印'BC =',data.b + data.c


Original question below. 原始问题如下。

I'm an old hand at C, but new to Python, so my perspective is likely misaligned. 我是C的老手,但对Python来说很新,所以我的观点很可能是错位的。 I need to call a function with a couple of variables, and a list of a predefined set of values. 我需要调用一个带有几个变量的函数,以及一组预定义值的列表。 I imagine something like this: 我想象这样的事情:

class TestData
    def __init__(self, input, delay, result)
    self.input = input
    self.delay = delay
    self.result = result

#                (input, delay, result)
TestData list = [
                 (11, 250, 42)
                 (22, 500, 123)
                 (37, 100, 97)
                ]

RunTest(id, channel, list)
    setup(id, channel)
    for each in list
        ...

Can this work? 这可以吗? Or am I headed in the wrong direction? 还是我朝错误的方向前进?

Please excuse me if I haven't entered the example code with the correct formatting. 如果我没有输入格式正确的示例代码,请原谅。

Setup 建立

class TestData:
     def __init__(self, inp, delay, result):
         self.input = inp
         self.delay = delay
         self.result = result

_list = [
  (11, 250, 42),
  (22, 500, 123),
  (37, 100, 97)
 ]

Use a list comprehension to create your objects, passing data at each iteration to the constructor: 使用列表推导来创建对象,在每次迭代时将数据传递给构造函数:

>>> [TestData(*x) for x in _list]
[<__main__.TestData at 0x140b47eb8>,
 <__main__.TestData at 0x140b472b0>,
 <__main__.TestData at 0x140b474e0>]

Or, in a similar fashion, using a for loop: 或者,以类似的方式,使用for循环:

>>> o_list = []
>>> for x in _list:
...    o_list.append(TestData(*x))
...
>>> o_list
[<__main__.TestData at 0x143d8a1d0>,
 <__main__.TestData at 0x143d8a2b0>,
 <__main__.TestData at 0x143d8a278>]

I can see a number of problems with the code you posted, I think you have more issues than just how to pass a list of values to a function. 我可以看到您发布的代码存在许多问题,我认为您遇到的问题不仅仅是如何将值列表传递给函数。

Your TestData class looks perfectly fine, but this code won't work: 你的TestData类看起来很好,但是这段代码不起作用:

TestData list = [
                 (11, 250, 42)
                 (22, 500, 123)
                 (37, 100, 97)
                ]

If you're trying to create a new instance of TestData , you'd want: 如果您正在尝试创建TestData的新实例,则需要:

list = TestData(
    (11, 250, 42),
    (22, 500, 123),
    (37, 100, 97)
    )

A couple notes here: 这里有几个笔记:

  1. You probably don't want to call a local variable list since that'll clash with the list() global list class. 您可能不希望调用局部变量list因为它将与list()全局list类冲突。
  2. You're not passing a list of arguments to the TestData constructor, you're sending 3 separate arguments, each of which is a tuple . 您没有将参数列表传递给TestData构造函数,而是发送3个单独的参数,每个参数都是一个tuple
  3. Tuples are basically immutable lists (you can't reassign values in a tupe, you can't resize a tuple), and are constructed with (a,b) whereas lists are constructed with [a,b] 元组基本上是不可变的列表(你不能在tupe中重新分配值,你不能调整元组的大小),并且用(a,b)构造,而列表用[a,b]构造

Next is this code: 接下来是这段代码:

RunTest(id, channel, list)
    setup(id, channel)
    for each in list

What is RunTest supposed to be? 什么是RunTest应该是什么? a function? 一个功能? a class? 一类? If it's a function, you'd want: 如果它是一个功能,你需要:

def RunTest(id, channel, list):

Note the leading def to define a function and the trailing : to start a new block of code. 注意前导def定义一个函数和尾随:启动一个新的代码块。 It's also python convention that only classes use CammelCase , functions should use snake_case . 它也是python惯例,只有类使用CammelCase ,函数应该使用snake_case

What does your setup function look like? 你的setup功能是什么样的? You don't give it here. 你不在这里给它。

The code for each in list needs a trailing colon and you shouldn't use each , it's not necessary for a python for loop. for each in list代码for each in list需要一个尾随冒号,你不应该使用each ,这对于python for循环来说并不是必需的。 The identifier in that position will have the element from each iteration over list . 该位置的标识符将包含每个迭代list的元素。 You probably want something like: 你可能想要这样的东西:

for item in list:
    do_something(item)

But that's also assuming that list is an iterable. 但这也是假设list是可迭代的。 If you really wanted list to be an instance of your TestData class, it's likely not iterable. 如果您真的希望list成为TestData类的实例,那么它可能无法迭代。 Did you want to loop over each item from list.input ? 你想从list.input循环遍历每个项目吗?

It can work, but not like that. 它可以工作,但不是那样。

tdparams = [ ... ]

 ...

tdseq = [TestData(*params) for params in tdparams]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM