问题描述
Python的新手,但我已经研究了几个小时了。如果我错过了一些明显的东西,请原谅我。
New to Python, but I have been researching this for a couple hours. Forgive me if I missed something obvious.
我有一个名为LineItem的类,它有一个属性_lineItems,属于给定LineItem的LineItem列表。一个子列表,基本上。
I have a class called LineItem, which has an attribute _lineItems, a list of LineItems that belong to the given LineItem. A sub-list, basically.
我想打印出一个LineItem及其所有子项(以及子项拥有的子项),但是我我遇到迭代问题。
I want to print out a LineItem and all of its sub-items (and the sub-items own sub-items), but I'm having trouble with the iteration.
from decimal import *
class LineItem(object):
"""
Instance attributes:
amount: Decimal
_lineItems: list of child internal lineitems (possibly an empty list)
isInternal: bool
"""
def __init__(self, **kw):
self.amount = Decimal(0)
self._lineItems = []
self.isInternal = False
for k, v in kw.items():
setattr(self, k, v)
一个让我麻烦的LineItem示例在下面定义为ext2,有三个孩子。
An example LineItem that's giving me the trouble is defined below as ext2, with three children.
# External line item with one level of children
int1 = LineItem(amount=Decimal('1886.75'), description='State Dues',
isInternal=True)
int2 = LineItem(amount=Decimal('232.50'), description='National Dues',
isInternal=True)
int3 = LineItem(amount=Decimal('50'), description='Processing Fee',
isInternal=True)
ext2 = LineItem(amount=Decimal('2169.25'), description='Dues',
_lineItems=[int1, int2, int3])
我有这个递归函数来迭代所有子项(并打印它们编号,如1,2,2.1作为第二项的第一个子项,等等)
I have this recursive function for iterating all the sub-items (and printing them numbered, like 1, 2, 2.1 as the first subitem of the second item, etc)
def print_line_item(LineItems):
count = 1
for a in LineItems:
print count, ' ', a.description, ' (', a.amount, ')'
if a._lineItems != []:
for b in a._lineItems:
print count, '.', print_line_item(b),
count+=1
但是当我尝试使用它时
def main():
print_line_item([ext1, ext2, ext3]) #ext1 has no children, prints fine
if __name__=="__main__":
main()
我得到
line 56, in print_line_item
print count, '.', print_line_item(b),
line 51, in print_line_item
for a in LineItems:
TypeError: 'LineItem' object is not iterable
好的,所以不知何故我搞砸了名单。
Okay, so somehow I'm screwing up lists.
如果我添加一个couple print:
if I add a couple print statements:
def print_line_item(LineItems):
count = 1
for a in LineItems:
print count, ' ', a.description, ' (', a.amount, ')'
if a._lineItems != []:
print a._lineItems
for b in a._lineItems:
print b
print count, '.', print_line_item(b),
count+=1
我得到证明a._lineItems确实是一个列表,打印如下s:
I get proof that a._lineItems is indeed a list, printed as follows:
[<__main__.LineItem object at 0x0227C430>, <__main__.LineItem object at 0x0227C5F0>, <__main__.LineItem object at 0x0227C670>]
并且b我正试图传递给递归调用是一个LineItem的内存地址
and that the b I'm trying to pass to the recursing call is a memory address of a single LineItem
<__main__.LineItem object at 0x0227C430>
那我该怎么做才能做我想做的事情?我用.iter或 ___ iter ___
尝试了几件事,但没有运气。
Sooo how am I actually supposed to do what I'm trying to do? I tried a couple things with .iter or ___iter___
but no luck.
另外,if a._lineItems! = []似乎也没有工作(也没有变化)。我得到打印行无
Also, the if a._lineItems != [] doesn't seem to be working either (nor variations on that). I get printed lines of "None"
推荐答案
def print_line_item(LineItems):
count = 1
for a in LineItems:
print count, ' ', a.description, ' (', a.amount, ')'
if a._lineItems != []:
for b in a._lineItems:
print count, '.', print_line_item(b),
count+=1
它可能是正确的版本,未经过测试。
It might be correct version, not tested.
def print_line_item(LineItems, precedingNumber='1'):
count = 1
for a in LineItems:
print precedingNumber, '.', count, ' ', a.description, ' (', a.amount, ')'
print_line_item(a._lineItems, precedingNumber + '.' + count),
count+=1
这篇关于对象列表的Python迭代“不可迭代”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!