python - 如何在 1d 列表中组合相同的匹配项并使其成为 2d 列表
问题描述
将相同的项目组合在一维列表中并使其成为二维列表。我可以使用for
循环和if
条件来做到这一点。但我想知道是否有任何pythonic方法可以做到这一点。
输入列表:
lis = ['computer', 'science', 'math', 'science', 'biology', 'math', 'computer', 'math']
输出应该是:
[['computer', ' computer'], ['science', 'science'], ['math', 'math', 'math'], ['biology'] ]
解决方案
推荐阅读
- javascript - 为什么 Puppeteer 工作的 headless 需要是 false 的?
- c++ - 为什么 tag_invoke 模式根本需要 Niebloid std::tag_invoke?
- linux - 在远程机器上以 root 身份在本地文件中无密码执行本地脚本
- java - Spring JUnit Test 加载 src/test/resources 之外文件的属性
- apache-spark - PySpark - 取 1 和 0 序列的第一个值
- google-cloud-firestore - 对 nosql 数据库进行建模以实现最大性能
- flutter - 如何自动刷新 Stream Builder
- python - 在特定列查找最小数字,然后返回该行
- html - 如何在显示 flex 容器内制作子容器显示块
- javascript - btn.setAttribute("onclick","myFunction(this.id)"); 剂量工作?