0% found this document useful (0 votes)
49 views3 pages

Project 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views3 pages

Project 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

users=[

{'id':0,'name':'Ashok'},
{'id':1, 'name':'Rahul'},
{'id':2, 'name':'Sarita'},
{'id':3, 'name':'Rohan'},
{'id':4, 'name':'Akhaya'},
{'id':5, 'name':'Prakash'},
{'id':6, 'name':'Madhabi'},
{'id':7, 'name':'Spandan'}

interests=[(0,'python'),(0,'java'),(1,'java'),(2,'python'),(2,'java'),(3,'python'),
(4,'c'),(5,'c'),
(6,'python'),(7,'java')]

# function that finds users with a certain interest

'''
target_interest: The interest to search for.
user_interests: A list of tuples where each tuple contains a user ID and their
interest.
It returns a list comprehension that filters user_interests based on
whether the target_interest matches the interest of the user in each tuple,
returning a list of user IDs who have the specified interest.
'''
def data_scientists_who_like(target_interest, user_interests):
return [
user_id
for user_id, user_interest in user_interests
if user_interest == target_interest
]

# Loop through interests


for interest_tuple in interests:
target_interest = interest_tuple[1] #This line extracts the second element
(index 1) from the
#interest_tuple, which is assumed to be the
interest itself.
#It assigns this interest to the variable
target_interest.
users_with_interest = data_scientists_who_like(target_interest, interests)
'''
interests: The entire list of interests. This list is passed to the function to
allow it to
search for users interested in the specified target_interest.
'''
print(f"Users interested in '{target_interest}': {users_with_interest}")

'''
Users interested in 'python': [0, 2, 3, 6]
Users interested in 'java': [0, 1, 2, 7]
Users interested in 'java': [0, 1, 2, 7]
Users interested in 'python': [0, 2, 3, 6]
Users interested in 'java': [0, 1, 2, 7]
Users interested in 'python': [0, 2, 3, 6]
Users interested in 'c': [4, 5]
Users interested in 'c': [4, 5]
Users interested in 'python': [0, 2, 3, 6]
Users interested in 'java': [0, 1, 2, 7]

'''

from collections import defaultdict


# Keys are interests, values are lists of user_ids with that interest
user_ids_by_interest = defaultdict(list)
for user_id, interest in interests:
user_ids_by_interest[interest].append(user_id)
print(user_ids_by_interest)

'''
defaultdict(<class 'list'>, {'python': [0, 2, 3, 6], 'java': [0, 1, 2, 7], 'c': [4,
5]})
'''
'''
Efficiency:

Code 2 iterates through the interests list only once to populate the
user_ids_by_interest dictionary.
In contrast, Code 1 requires iterating through the interests list once for each
target interest
when calling the data_scientists_who_like() function within the loop.
This could lead to redundant iterations, especially if there are many interests.
Data Organization:

Code 2 directly organizes users by interest into a dictionary


(user_ids_by_interest), where
interests serve as keys and lists of corresponding user IDs serve as values.
This organized structure makes it easier and more efficient to access users by
their
interests later in the code without needing to repeatedly search the entire list.

'''
# # Keys are user_ids, values are lists of interests for that user_id.
interests_by_user_id = defaultdict(list)
for user_id, interest in interests:
interests_by_user_id[user_id].append(interest)
print(interests_by_user_id)
'''
defaultdict(<class 'list'>, {0: ['python', 'java'], 1: ['java'], 2: ['python',
'java'],
3: ['python'], 4: ['c'], 5: ['c'], 6: ['python'], 7:
['java']})
'''

from collections import Counter


def most_common_interests_with(user):
return Counter(
interested_user_id
for interest in interests_by_user_id[user["id"]]
for interested_user_id in user_ids_by_interest[interest]
if interested_user_id != user["id"]
)
res=[most_common_interests_with(user) for user in users]
print(res)
'''
[Counter({2: 2, 3: 1, 6: 1, 1: 1, 7: 1}),
Counter({0: 1, 2: 1, 7: 1}),
Counter({0: 2, 3: 1, 6: 1, 1: 1, 7: 1}),
Counter({0: 1, 2: 1, 6: 1}),
Counter({5: 1}),
Counter({4: 1}),
Counter({0: 1, 2: 1, 3: 1}),
Counter({0: 1, 1: 1, 2: 1})]
'''

You might also like