简体   繁体   English

如何迭代列表的元组?

[英]How to iterate a tuple of lists?

alright this might be a stupid one but I am kinda stuck right now. 好吧,这可能是一个愚蠢的想法,但是我现在还停留在那儿。 I am dealing with arguments of type tuple, containing lists of lists and just can't read them properly. 我正在处理元组类型的参数,其中包含列表列表,只是无法正确读取它们。

Here is a basic example: 这是一个基本示例:

def test(*args):
    for arg in args:
        print(arg)
        print(arg[0])
        print(arg[1])

testTuple1 = ([["x_1", "x_2", "x_3"], []])
testTuple2 = ([['x_1', 'x_2', 'x_3'], []], [['x_2', 'x_3'], ['x_1']])

test(testTuple1)
test(testTuple2)

Which results in the following outputs: 结果为以下输出:

[['x_1', 'x_2', 'x_3'], []]
['x_1', 'x_2', 'x_3']
[]

([['x_1', 'x_2', 'x_3'], []], [['x_2', 'x_3'], ['x_1']])
[['x_1', 'x_2', 'x_3'], []]
[['x_2', 'x_3'], ['x_1']]

The output of the first example is the output I would have expected. 第一个示例的输出是我期望的输出。 However I don't get the first output of the second example. 但是我没有得到第二个示例的第一个输出。 Why am I getting a tuple and not the first of the lists contained in the tuple: [['x_1', 'x_2', 'x_3'], []]? 为什么我得到一个元组而不是元组中包含的第一个列表:[['x_1','x_2','x_3'],[]]?

Because in python there's some ambiguity created by the fact that parens are used both for creating literal tuples and for general grouping of expressions. 因为在python中,parens既用于创建文字元组,又用于表达式的常规分组,因此产生了一些歧义。 Here's your lists, with some added formatting for clarity: 这是您的列表,为清晰起见添加了一些格式:

testTuple1 = (
               [
                 ["x_1", "x_2", "x_3"], 
                 []
               ]
             )

testTuple2 = (
               [
                 ['x_1', 'x_2', 'x_3'], 
                 []
               ], 
               [
                 ['x_2', 'x_3'], 
                 ['x_1']
               ]
             )

testTuple1 , since the outer parens contain only a single item (the outermost list), will evaluate to just that outer list. testTuple1 ,因为外部括号仅包含一个项目(最外部的列表),因此将只求该外部列表的值。 The parens just sort of go away. parens消失了。 But in testTuple2 , since there are multiple comma-separated lists, it will evaluate to a tuple. 但是在testTuple2 ,由于存在多个逗号分隔的列表,因此它将评估为一个元组。

If you want the first one to be a tuple as well, you need to add a trailing comma after the outer list to explicitly let python know that those parens are meant to denote a 1-tuple, and aren't just for grouping: 如果您还希望第一个也是元组,则需要在外部列表后添加尾随逗号,以明确地让python知道这些括号是用来表示1元组的,而不仅仅是用于分组:

testTuple1 = ([["x_1", "x_2", "x_3"], []],)

When you use the asterisk as modifier for your argument, it will enable you to pass arbitrarily many arguments when calling the function. 当您使用星号作为参数的修饰符时,它将使您在调用函数时可以传递任意多个参数。

def test(*args):
    # Do anything

test(v1)
test(v1, arg, another, something)
test()

It will end up concatenate all arguments from the call into one tuple. 它将最终将调用中的所有参数连接到一个元组中。

When you simply pass one argument to the function call, the tupled argument will end consisting only of this one argument, no matter what class this object actually has. 当您仅将一个参数传递给函数调用时,无论该对象实际上具有什么类,元组参数都将仅由该参数构成。 Just do the printing right before the iteration. 只需在迭代之前进行打印即可。 Then you will see the actual value of args , which resolves to this: 然后,您将看到args的实际值,它解析为:

([['x_1', 'x_2', 'x_3'], []],)


If you want to pass an iterable to a function and have the element on the first layer spread out to the arguments just use the asterisk at the call again. 如果您想将一个可迭代对象传递给一个函数,并将第一层的元素扩展到参数中,只需在调用处再次使用星号即可。

>> test(*testTuple1)
['x_1', 'x_2', 'x_3']
'x_1'
'x_2'
[]

IndexError: ....

The IndexError occurrs, because an empty list does not have a zero-th or even a first element to get. 发生IndexError的原因是,空列表没有第零个或什至第一个要获取的元素。

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

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