

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find same contacts in a list of contacts in Python
Suppose we have a list of contacts holding username, email and phone number in any order, we have to find the same contacts (When same person have many different contacts) and return the same contacts together. We have to keep in mind that −
A contact can store username, email and phone fields according to any order.
Two contacts are same if they have either same username or same email or same phone number.
So, if the input is like Contacts = [{"Amal", "amal@gmail.com", "+915264"},{ "Bimal", "bimal321@yahoo.com", "+1234567"},{ "Amal123", "+1234567", "amal_new@gmail.com"},{ "AmalAnother", "+962547", "amal_new@gmail.com"}], then the output will be [0,2,3], [1] as contacts at index [0,2,3] are same, and another contact at index 1.
To solve this, we will follow these steps −
Define a function generate_graph() . This will take cnt, n, matrix
for i in range 0 to n, do
for j in range 0 to n, do
matrix[i, j] := 0
for i in range 0 to n, do
for j in range i + 1 to n, do
if cnt[i].slot1 is same as cnt[j].slot1 or cnt[i].slot1 is same as cnt[j].slot2 or cnt[i].slot1 is same as cnt[j].slot3 or cnt[i].slot2 is same as cnt[j].slot1 or cnt[i].slot2 is same as cnt[j].slot2 or cnt[i].slot2 is same as cnt[j].slot3 or cnt[i].slot3 is same as cnt[j].slot1 or cnt[i].slot3 is same as cnt[j].slot2 or cnt[i].slot3 is same as cnt[j].slot3, then
matrix[i, j] := 1
matrix[j, i] := 1
come out from the loop
Define a function visit_using_dfs() . This will take i, matrix, visited, sol, n
visited[i] := True
insert i at the end of sol
for j in range 0 to n, do
if matrix[i][j] is non-zero and not visited[j] is non-zero, then
visit_using_dfs(j, matrix, visited, sol, n)
From the main method, do the following −
n := size of cnt
sol := a new list
matrix := make a square matrix of size n x n
visited := make an array of size n, and fill with 0
generate_graph(cnt, n, matrix)
for i in range 0 to n, do
if not visited[i] is non-zero, then
visit_using_dfs(i, matrix, visited, sol, n)
insert -1 at the end of sol
for i in range 0 to size of sol, do
if sol[i] is same as -1, then
go to the next line
otherwise,
display sol[i]
Example
Let us see the following implementation to get better understanding −
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)
Input
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")]
Output
0 2 3 1
- Related Questions & Answers
- How to read all contacts in android?
- How to add new contacts in Android App?
- 6 Simple Ways to Transfer your Mobile Contacts
- How to add new contacts in Android App using Kotlin?
- Simple Steps to make your Google Contacts upto Date
- Program to check whether we can stand at least k distance away from the closest contacts in Python
- Find mismatch item on same index in two list in Python
- Find average of a list in python?
- Find size of a list in Python
- Python - Check if all elements in a List are same
- Python Group elements at same indices in a multi-list
- Find Mean of a List of Numpy Array in Python
- Find elements of a list by indices in Python
- How to find sum a list of numbers in Python?
- Find groups of strictly increasing numbers in a list in Python