使用 Python 在联系人列表中查找相同的联系人

pythonserver side programmingprogramming更新于 2025/9/27 4:28:17

假设我们有一个联系人列表,其中包含用户名、电子邮件和电话号码,顺序任意,我们必须找到相同的联系人(当同一个人有许多不同的联系人时)并一起返回相同的联系人。我们必须记住 −

  • 联系人可以按照任何顺序存储用户名、电子邮件和电话字段。

  • 如果两个联系人具有相同的用户名、相同的电子邮件或相同的电话号码,则它们相同。

因此,如果输入如下 Contacts = [{"Amal", "amal@gmail.com", "+915264"},{ "Bimal", "bimal321@yahoo.com", "+1234567"},{ "Amal123", "+1234567", "amal_new@gmail.com"},{ "AmalAnother", "+962547", "amal_new@gmail.com"}],则输出将为 [0,2,3], [1],因为索引 [0,2,3] 处的联系人相同,索引 1 处有另一个联系人。

为了解决这个问题,我们将遵循以下步骤 −

  • 定义一个函数 generate_graph() 。这将需要 cnt、n、矩阵

  • 对于范围在 0 到 n 内的 i,执行

    • 对于范围在 0 到 n 内的 j,执行

      • matrix[i, j] := 0

  • 对于范围在 0 到 n 内的 i,执行

    • 对于范围在 i + 1 到 n 内的 j,执行

      • 如果 cnt[i].slot1 与 cnt[j].slot1 相同或 cnt[i].slot1 与 cnt[j].slot2 相同或 cnt[i].slot1 与 cnt[j].slot3 相同或 cnt[i].slot2 与cnt[j].slot1 或 cnt[i].slot2 与 cnt[j].slot2 相同或 cnt[i].slot2 与 cnt[j].slot3 相同或 cnt[i].slot3 与 cnt[j].slot1 相同或 cnt[i].slot3 与 cnt[j].slot2 相同或 cnt[i].slot3 与 cnt[j].slot3 相同,则

        • matrix[i, j] := 1

        • matrix[j, i] := 1

        • 跳出循环

  • 定义一个函数 visit_using_dfs() 。这将需要 i、matrix、visited、sol、n

  • visited[i] := True

  • 在 sol 末尾插入 i

  • 对于 0 到 n 范围内的 j,执行

    • 如果 matrix[i][j] 非零且 not accessed[j] 非零,则

      • visit_using_dfs(j, matrix, accessed, sol, n)

  • 从主方法中,执行以下操作 −

  • n := cnt 的大小

  • sol := 一个新列表

  • matrix :=创建一个大小为 n x n 的方阵

  • visited := 创建一个大小为 n 的数组,并用 0 填充

  • generate_graph(cnt, n, matrix)

  • 对于范围在 0 到 n 内的 i,执行

    • 如果 not accessed[i] 非零,则

      • visit_using_dfs(i, matrix, accessed, sol, n)

      • 在 sol 末尾插入 -1

  • 对于范围在 0 到 sol 大小内的 i,执行

    • 如果 sol[i] 与 -1 相同,然后

      • 转到下一行

    • 否则,

      • 显示 sol[i]

示例

让我们看看下面的实现以便更好地理解 −

class contact:
   def __init__(self, slot1, slot2, slot3):
      self.slot1 = slot1
      self.slot2 = slot2
      self.slot3 = slot3
def generate_graph(cnt, n, matrix):
   for i in range(n):
      for j in range(n):
         matrix[i][j] = 0
   for i in range(n):
      for j in range(i + 1, n):
         if (cnt[i].slot1 == cnt[j].slot1 or cnt[i].slot1 == cnt[j].slot2 or cnt[i].slot1 == cnt[j].slot3 or cnt[i].slot2 == cnt[j].slot1 or cnt[i].slot2 == cnt[j].slot2 or cnt[i].slot2 == cnt[j].slot3 or cnt[i].slot3 == cnt[j].slot1 or cnt[i].slot3 == cnt[j].slot2 or cnt[i].slot3 == cnt[j].slot3):
            matrix[i][j] = 1
            matrix[j][i] = 1
            break
def visit_using_dfs(i, matrix, visited, sol, n):
   visited[i] = True
   sol.append(i)
   for j in range(n):
      if (matrix[i][j] and not visited[j]):
         visit_using_dfs(j, matrix, visited, sol, n)
def get_similar_contacts(cnt):
   n = len(cnt)
   sol = []
   matrix = [[None] * n for i in range(n)]
   visited = [0] * n
   generate_graph(cnt, n, matrix)
   for i in range(n):
      if (not visited[i]):
         visit_using_dfs(i, matrix, visited, sol, n)
         sol.append(-1)
   for i in range(len(sol)):
      if (sol[i] == -1):
         print()
      else:
         print(sol[i], end = " ")

cnt = [contact("Amal", "amal@gmail.com", "+915264"),
   contact("Bimal", "bimal321@yahoo.com", "+1234567"),
   contact("Amal123", "+915264", "amal_new@gmail.com"),
   contact("AmalAnother", "+962547", "amal_new@gmail.com")]
get_similar_contacts(cnt)

输入

cnt = [contact("Amal", "amal@gmail.com", "+915264"),
contact("Bimal", "bimal321@yahoo.com", "+1234567"),
contact("Amal123", "+915264", "amal_new@gmail.com"),
contact("AmalAnother", "+962547", "amal_new@gmail.com")]

输出

0 2 3
1

相关文章


有用资源