Merging a list of lists

前端 未结 4 900
醉酒成梦
醉酒成梦 2020-12-19 08:05

How do I merge a list of lists?

[[\'A\', \'B\', \'C\'], [\'D\', \'E\', \'F\'], [\'G\', \'H\', \'I\']]

into

[\'A\', \'B\', \         


        
4条回答
  •  甜味超标
    2020-12-19 08:14

    Use itertools.chain:

    >>> import itertools
    >>> list(itertools.chain(*mylist))
    ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
    

    Wrapping the elements in HTML can be done afterwards.

    >>> ['' + x + '' for x in itertools.chain(*mylist)]
    ['A', 'B', 'C', 'D', 'E', 'F',
    'G', 'H', 'I']
    

    Note that if you are trying to generate valid HTML you may also need to HTML escape some of the content in your strings.

提交回复
热议问题