在集合列表中查找重复项的计数

发布于 2025-02-05 03:38:33 字数 668 浏览 3 评论 0原文

嗨,我必须从数据中检查字符串的重叠和重复,我可以这样做,任何人都可以帮助我找到字符串的副本。我有此数据s = [(100,350,“ a”), (125,145,“ a”),(200,400,“ d”),(0,10,“ a”)],我完成了重叠部分,但是请检查ODF字符串,我需要帮助。

def overlap(a, b) -> bool:
  a_start, a_end, _ = a
  b_start, b_end, _ = b
  return a_start < b_end and b_start < a_end
ls = [(100, 350,"a"), (125, 145,"a"), (200, 400, "d"), (0, 10, "a")]
overlaps = set()
for idx_a in range(len(ls)):
    for idx_b in range(len(ls)):
        if idx_a != idx_b:  
            if overlap(ls[idx_a], ls[idx_b]):
                overlaps.add(ls[idx_a])
                overlaps.add(ls[idx_b])

print(f"Number of overlaps: {len(overlaps)}")

Hi i have to check the overlapping and duplicate of string from the data , i could do it can anyone help me to find the duplicate of string .I have this data s = [(100, 350,"a"), (125, 145,"a"), (200, 400, "d"), (0, 10, "a")] and i done the overlap part but theduplicate check odf string i need help .

def overlap(a, b) -> bool:
  a_start, a_end, _ = a
  b_start, b_end, _ = b
  return a_start < b_end and b_start < a_end
ls = [(100, 350,"a"), (125, 145,"a"), (200, 400, "d"), (0, 10, "a")]
overlaps = set()
for idx_a in range(len(ls)):
    for idx_b in range(len(ls)):
        if idx_a != idx_b:  
            if overlap(ls[idx_a], ls[idx_b]):
                overlaps.add(ls[idx_a])
                overlaps.add(ls[idx_b])

print(f"Number of overlaps: {len(overlaps)}")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入画浅相思 2025-02-12 03:38:33

似乎您不需要使用设置,以防您只需要重叠数量。

我会这样解决您的问题:

def is_overlapped(a, b) -> bool:  # changed the name for readability
    a_start, a_end, _ = a
    b_start, b_end, _ = b
    return a_start < b_end and b_start < a_end
ls = [(100, 350,"a"), (125, 145,"a"), (200, 400, "d"), (0, 10, "a")]
overlaps = 0 # int instead of set
for idx_a, value_a in enumerate(ls):  # this is more pythonic way to access the index and item at the same time
    for idx_b, value_b in enumerate(ls):
        if idx_a != idx_b:
            if is_overlapped(value_a, value_b):
                overlaps += 1
print(f"Number of overlaps: {overlaps}")

It seems like you don't need to use set in case you need only number of overlaps.

I would solve your problem like this:

def is_overlapped(a, b) -> bool:  # changed the name for readability
    a_start, a_end, _ = a
    b_start, b_end, _ = b
    return a_start < b_end and b_start < a_end
ls = [(100, 350,"a"), (125, 145,"a"), (200, 400, "d"), (0, 10, "a")]
overlaps = 0 # int instead of set
for idx_a, value_a in enumerate(ls):  # this is more pythonic way to access the index and item at the same time
    for idx_b, value_b in enumerate(ls):
        if idx_a != idx_b:
            if is_overlapped(value_a, value_b):
                overlaps += 1
print(f"Number of overlaps: {overlaps}")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文