0
我有一個對象列表,每個對象都有一個列表。我希望創建所有對象中所有東西的列表。是否有更多pythonic這樣做?創建來自對象項目的項目列表
class Holder(object):
def __init__(self, things):
self.things = things
holder_one= Holder([1, 2])
holder_two = Holder(['a', 'b'])
holders = [holder_one, holder_two]
all_things = []
for holder in holders:
for thing in holder.things:
all_things.append(thing)
print all_things
感謝,但真正的Holder類會做的遠不止持有的列表。這個例子對於這個問題的目的非常簡單,持有人繼承列表 – Psionman
確實不太可行,所以使用'extend' .. – DeepSpace
謝謝。訣竅嗎 – Psionman