在 Python 中查詢聯絡人列表中的相同聯絡人
假設我們有一個聯絡人列表,其中包含以任意順序儲存的使用者名稱、電子郵件和電話號碼,我們需要找到相同的聯絡人(當同一個人有多個不同的聯絡方式時),並將相同的聯絡人放在一起返回。我們需要記住以下幾點:-
一個聯絡人可以根據任何順序儲存使用者名稱、電子郵件和電話欄位。
如果兩個聯絡人具有相同的使用者名稱、電子郵件或電話號碼,則它們是相同的。
因此,如果輸入類似於 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、matrix 作為引數。
對於 i 的範圍從 0 到 n,執行以下操作:
對於 j 的範圍從 0 到 n,執行以下操作:
matrix[i, j] := 0
對於 i 的範圍從 0 到 n,執行以下操作:
對於 j 的範圍從 i + 1 到 n,執行以下操作:
如果 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
將 i 插入到 sol 的末尾。
對於 j 的範圍從 0 到 n,執行以下操作:
如果 matrix[i][j] 不為零,並且 visited[j] 不為零,則:
visit_using_dfs(j, matrix, visited, sol, n)
從主方法中執行以下操作:-
n := cnt 的大小
sol := 一個新的列表
matrix := 建立一個大小為 n x n 的方陣
visited := 建立一個大小為 n 的陣列,並填充 0
generate_graph(cnt, n, matrix)
對於 i 的範圍從 0 到 n,執行以下操作:
如果 visited[i] 不為零,則:
visit_using_dfs(i, matrix, visited, sol, n)
將 -1 插入到 sol 的末尾。
對於 i 的範圍從 0 到 sol 的大小,執行以下操作:
如果 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
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP