简体   繁体   English

使用 indeces 迭代元组列表理解

[英]iterate over tuple list comprehension using indeces

I'm trying to iterate over a list comprehension tuple using indeces to reference each tuple and its associated length and then printing out each element within the tuple list, but python is not accepting this.我正在尝试使用 indeces 来遍历列表理解元组以引用每个元组及其相关长度,然后打印出元组列表中的每个元素,但是 python 不接受这一点。 I'm a comp sci major but about 10 years ago and we learned off C++ and Java, so am still new to python:我是 Comp sci 专业,但大约 10 年前,我们学习了 C++ 和 Java,所以我还是 Python 新手:

q = 0    
productResourcePairs = [(x, y) for x in [1,2,3] for y in [3,1,4]]
    for p in productResourcePairs:
        for i in range(len(productResourcePairs(p))):
             print(productResourcePairs[q][i])
        q = q + 1

I'm simply trying to get the length of each tuple and then print out using [][] its correspdoning value and then moving onto the next tuple.我只是想获取每个元组的长度,然后使用 [][] 其相关值打印出来,然后移至下一个元组。

Python throws the following error: Python 抛出以下错误:

Traceback (most recent call last): for i in range(len(productResourcePairs(p))): TypeError: 'list' object is not callable回溯(最近一次调用最后一次):for i in range(len(productResourcePairs(p))): TypeError: 'list' object is not callable

It's working now and will close this out just noting the fixes and the bottom calls yield the same result with respect to the tuple:它现在正在工作,并将关闭它,只需注意修复和底部调用对元组产生相同的结果:

for p in productResourcePairs:

    for i in range(len(p)):
        print(p[i])
        print(productResourcePairs[q][i])
    q = q + 1

() are the call operator, so productResourcePairs(p) is trying to call your list - which makes no sense since lists are not callable. ()是调用运算符,因此productResourcePairs(p)正在尝试调用您的列表 - 这没有任何意义,因为列表不可调用。 productResourcePairs[p] is trying to get the element at index p of your list - but here p is a tuple , not an int , and you obviously cannot index a list with a tuple. productResourcePairs[p]试图获取列表索引p处的元素 - 但这里p是一个tuple ,而不是一个int ,并且您显然不能用元组索引列表。

Now I'm not really sure what you're trying to do, but if the goal is to print each element of each tuple in your list, then you're making things much more complicated than they have to be:现在我不确定您要做什么,但是如果目标是打印列表中每个元组的每个元素,那么您会使事情变得比它们必须复杂得多:

productResourcePairs = [(x, y) for x in [1,2,3] for y in [3,1,4]]
for p in productResourcePairs:
    for item in p:
        print(item)

I kindly suggest you do the full official Python tutorial, it covers all those points.我建议你做完整的官方 Python 教程,它涵盖了所有这些要点。

EDIT :编辑

if you absolutely insist on using indexed access, even though it's both unpythonic, uselessly complicated and much less efficient (for the above use case), you can of course still do so:如果您绝对坚持使用索引访问,即使它既非pythonic,又无用的复杂且效率低得多(对于上述用例),您当然仍然可以这样做:

productResourcePairs = [(x, y) for x in [1,2,3] for y in [3,1,4]]
for p in productResourcePairs:
    plen = len(p)
    for i in range(plen):
        print(item[i])

or if you want to do even worse:或者如果你想做得更糟:

productResourcePairs = [(x, y) for x in [1,2,3] for y in [3,1,4]]
llen = len(productResourcePairs) 
for i in range(llen):
    plen = len(productResourcePairs[i])
    for j in range(plen):
        print(productResourcePairs[i][j])

Note that any experimented Python user would immediatly replace this code with my first snippet if they were to find this in the code base they're working on.请注意,任何经过试验的 Python 用户如果在他们正在处理的代码库中找到这段代码,都会立即用我的第一个代码段替换这段代码。

This is the correct code:这是正确的代码:

for p in productResourcePairs:

    for i in range(len(p)):
        print(p[i])
        print(productResourcePairs[q][i])
    q = q + 1

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

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