ZhangZhihui's Blog  

 

>>> bugs = ["bug1", "bug2"]
>>> sum(bugs, [])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
>>> sum([b for b in bugs], [])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
>>> sum((b for b in bugs), [])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
>>> sum([[b] for b in bugs], [])
['bug1', 'bug2']
>>> sum(([b] for b in bugs), [])
['bug1', 'bug2']
>>> sum(([b] for b in bugs), ["bug"])
['bug', 'bug1', 'bug2']

 

>>> bugs = [Bug.create() for _ in range(5)]
>>> sum((b.fission() for b in bugs), [])   #  fission() returns a list of Bug instances

 

posted on 2024-08-11 19:52  ZhangZhihuiAAA  阅读(7)  评论(0编辑  收藏  举报