python - 避免 for 循环中的重复计数
问题描述
这是我的情况,我在一个列表中有 16 个元组。我需要匹配元组的元素之一并迭代列表。它应该返回给我唯一的集合,但它返回给我双倍或三倍的计数。对此有任何列表理解建议吗?
lst= [(12, 'a'), (15, 'a'), (17, 'a'), (12, 'a'), (15, 'a'), (23, 'b'), (12, 'b'), (18, 'b'), (12, 'b'), (12, 'b'), (15, 'a'), (12, 'a'), (15, 'a'), (15, 'a'), (24, 'c'), (12, 'c')]
我需要从上面访问这个列表,
for i in employerEmployeeEdges:
""iterate over the list of tuples""
for j in employerEmployeeEdges[1:]:
"""iterate over to the next tuple to check if that matches
if i[1]==j[1]:
print(i[1], j[1))
我知道我已经部分完成了匹配项目,但我将如何防止重复计算?
解决方案
这将只给出唯一的元组:
import numpy as np
y = np.unique(lst, axis=0)
z = []
for i in y:
z.append(tuple(i))
推荐阅读
- python - 如何在excel表之间切换?
- php - 即使在页面刷新后 iframe 视频也不会停止运行
- c# - 在 Visual Studio 的 c#projects 中工作但在 Python 中不工作的 xml 肥皂代码
- ubuntu - 运行“./worldserver”显示“地图文件'./maps/0004331.map':不存在!” 错误
- javascript - 删除值时将输入背景颜色更改为白色
- java - 将数据从一个活动传递到另一个活动并设置为调用它的特定 EditText 字段
- curl - 将 Repos 添加到 Github 中的团队
- javascript - 对 MongoDB 的请求、数据聚合和进一步向 MongoDB 写入数据
- c# - 循环被触发 DataAnnotations
- python - 通过 value_counts() 将数据帧划分为两个 DF