Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:打印所有namedtuple_Python_Namedtuple - Fatal编程技术网

Python:打印所有namedtuple

Python:打印所有namedtuple,python,namedtuple,Python,Namedtuple,我有以下代码: from collections import namedtuple Test = namedtuple('Test', ['number_A', 'number_B']) test_1 = Test(number_A=1, number_B=2) test_2 = Test(number_A=3, number_B=4) test_3 = Test(number_A=5, number_B=6) 我的问题是如何打印所有命名的元组,例如: print (Test.numbe

我有以下代码:

from collections import namedtuple

Test = namedtuple('Test', ['number_A', 'number_B'])

test_1 = Test(number_A=1, number_B=2)
test_2 = Test(number_A=3, number_B=4)
test_3 = Test(number_A=5, number_B=6)
我的问题是如何打印所有命名的元组,例如:

print (Test.number_A)
我想看到这样的结果:

1
3
5
有什么想法吗?
谢谢。

Martijn Peters在评论中建议:


不要使用编号的变量。使用列表存储所有已命名的 而不是元组。只需在列表上循环以打印所有包含的命名 元组

下面是它的样子:

Test = namedtuple('Test', ['number_A', 'number_B'])
tests = []
tests.append(Test(number_A=1, number_B=2))
tests.append(Test(number_A=3, number_B=4))
tests.append(Test(number_A=5, number_B=6))

for test in tests:
    print test.number_A
给出:

1
3
5
以下是一个例子:

import collections

#Create a namedtuple class with names "a" "b" "c"
Row = collections.namedtuple("Row", ["a", "b", "c"], verbose=False, rename=False)   

row = Row(a=1,b=2,c=3) #Make a namedtuple from the Row class we created

print (row)    #Prints: Row(a=1, b=2, c=3)
print (row.a)  #Prints: 1
print (row[0]) #Prints: 1

row = Row._make([2, 3, 4]) #Make a namedtuple from a list of values

print (row)   #Prints: Row(a=2, b=3, c=4)

…从-

这应该向您展示如何:

>>> from collections import namedtuple
>>> Test = namedtuple('Test', ['number_A', 'number_B'])
>>> test_1 = Test(number_A=1, number_B=2)
>>> test_2 = Test(number_A=3, number_B=4)
>>> test_3 = Test(number_A=5, number_B=6)
>>> lis = [x.number_A for x in (test_1, test_2, test_3)]
>>> lis
[1, 3, 5]
>>> print "\n".join(map(str, lis))
1
3
5
>>>
实际上,最好的选择是使用列表而不是编号的变量:

>>> from collections import namedtuple
>>> Test = namedtuple('Test', ['number_A', 'number_B'])
>>> lis = []
>>> lis.append(Test(number_A=1, number_B=2))
>>> lis.append(Test(number_A=3, number_B=4))
>>> lis.append(Test(number_A=5, number_B=6))
>>> l = [x.number_A for x in lis]
>>> l
[1, 3, 5]
>>> print "\n".join(map(str, l))
1
3
5
>>>

您可以驱动跟踪其实例的子类:

from collections import namedtuple

_Test = namedtuple('_Test', ['number_A', 'number_B'])

class Test(_Test):
    _instances = []
    def __init__(self, *args, **kwargs):
        self._instances.append(self)
    def __del__(self):
        self._instances.remove(self)
    @classmethod
    def all_instances(cls, attribute):
        for inst in cls._instances:
            yield getattr(inst, attribute)

test_1 = Test(number_A=1, number_B=2)
test_2 = Test(number_A=3, number_B=4)
test_3 = Test(number_A=5, number_B=6)

for value in Test.all_instances('number_A'):
    print value
输出:

1
3.
5.

不要使用带编号的变量。使用列表来存储所有命名元组。只需在列表上循环即可打印所有包含的命名元组。