python - 如何在python中找到具有重复元素的元组?
问题描述
我有一个格式的列表
unique_edges=[('SLT2', 'SDP1'),('GCD7', 'ATG34'),('MTH1', 'MTH1'),('ADY2', 'ADY2')]
我需要使用列表理解(一行代码)将具有相同元素的元组两次(如('MTH1','MTH1'))移动到新列表。
我想我需要使用类似的东西
homo_dimers = list(map(tuple,unique_edges))
但我不知道如何使用这些函数在一个元组中搜索重复的元素。
解决方案
List comprehension to find tuples with same element:
homo_dimers = [(a, b) for a, b in unique_edges if a == b]
print(homo_dimers)
Prints:
[('MTH1', 'MTH1'), ('ADY2', 'ADY2')]
Or if your tuples contain more than 2 elements:
homo_dimers = [t for t in unique_edges if len(set(t)) == 1]
推荐阅读
- python - 了解 FHIR 的工作原理以及如何在医疗保健应用程序中实施它
- django - 为什么我不能在 Ubuntu 服务器上运行的 Django 中上传和保存图像?
- sonarqube-web - 如何通过 web api 获取 Sonarqube Metrics(即漏洞:A;B;C;D;E)评级
- spring-boot - 在docker上部署spring boot应用程序时出现此错误
- php - php如何按天对表中的数据进行分组
- javascript - 比较两个字段的值得到另一个字段的大值
- java - Jersey 1x 不在请求中发送 Content-Length 标头
- python - 有没有办法使用 Python 和 Selenium 从在线 PDF 中刮取信息而无需下载?
- python - 如何使用 Transcrypt 读取 URL 的内容?urlopen() 位于哪里?
- http - http到https端口转发是漏洞吗?