首页 > 解决方案 > 如何在 1d 列表中组合相同的匹配项并使其成为 2d 列表

问题描述

将相同的项目组合在一维列表中并使其成为二维列表。我可以使用for循环和if条件来做到这一点。但我想知道是否有任何pythonic方法可以做到这一点。

输入列表:

 lis = ['computer', 'science', 'math', 'science', 'biology', 'math', 'computer', 'math']

输出应该是:

[['computer', ' computer'], ['science', 'science'], ['math', 'math', 'math'], ['biology'] ]

标签: python

解决方案


推荐阅读