python - 比较不同List python中两个元组的项目
问题描述
你好朋友我需要你的帮助。我想在两个元组列表之间进行比较,如果两个元组之间有多个相同的值,我打印这个结果 exp:
L1 = [('G', 'T'), ('T', 'T'), ('T', 'U'), ('U', 'I'), ('I', 'P')]
L2 = [('E', 'G'), ('G', 'T'), ('T', 'P')]
输出:[0,1]
解决方案
使用列表理解:
L1 = [('G', 'T'), ('T', 'T'), ('T', 'U'), ('U', 'I'), ('I', 'P')]
L2 = [('E', 'G'), ('G', 'T'), ('T', 'P')]
indices = [[L1.index(s),i] for i, s in enumerate(L2) if s in L1]
# print the first match (in this case there is only one match)
print(indices[0])
[0, 1]
解释[[L1.index(s),i] for i, s in enumerate(L2) if s in L1]
:
for i, s in enumerate(L2)
: i 是索引,s 在 L2 的元组元素中if s in L1
:这会检查电流s
是否也在 L1 中[L1.index(s),i]
:这将返回索引列表
PS:对于重复项,这可能表现不佳。
推荐阅读
- string - terraform中字符串类型的最大大小是多少
- html - 引导表单中的按钮在小屏幕上溢出 div
- python - python中的特定打印语句包装器忽略输出或错误消息
- material-ui - 在主题中调整步进器步骤连接器完成状态
- jmeter - 如何从 shell 脚本关闭 JMeter IDE?
- matplotlib - 如何在特定点而不是在负时反转 matplotlib 条?
- elixir - 模块 Bcrypt.Base 不可用
- orm - 对于 flaskapp_builder 中的 SQLAInterface,如何使用“或”关系进行过滤?
- typescript - 控制流不使用联合元组进行类型缩小?
- python - Python 中条件语句的数量有限制吗?