>>> 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
标签:Python,list,sum,bug2,bugs,bug1,only From: https://www.cnblogs.com/zhangzhihui/p/18353816