合并清单清单 [英] Merging a list of lists
本文介绍了合并清单清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何合并列表列表?
[['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]
进入
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
如果我可以在合并列表(例如html标签)之前在每个项目的开头和结尾添加一个值,那就更好了.
Even better if I can add a value on the beginning and end of each item before merging the lists, like html tags.
即最终结果将是:
['<tr>A</tr>', '<tr>B</tr>', '<tr>C</tr>', '<tr>D</tr>', '<tr>E</tr>', '<tr>F</tr>', '<tr>G</tr>', '<tr>H</tr>', '<tr>I</tr>']
推荐答案
要连接列表,可以使用sum
To concatenate the lists, you can use sum
values = sum([['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']], [])
要添加HTML标签,可以使用列表推导.
To add the HTML tags, you can use a list comprehension.
html_values = ['<tr>' + i + '</tr>' for i in values]
这篇关于合并清单清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文