這應該告訴你如何:
>>> 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
>>>
請勿使用編號變量。使用* list *來存儲所有命名的元組。只需遍歷列表即可打印所有包含的命名元組。 –