Question

Hi, I have a python coding questions and would need help: Count the neighbors of each node in a g...

Hi, I have a python coding questions and would need help:

Count the neighbors of each node in a graph. input graph is a multi-dimensional list

Let's say I want to have each person's friends count:

input (list of list): [[A, B], [B, C], [B, D], [E]],

output (dict): {A:1, B: 3, C:1, D:1, E:0}

assume you won't get a repeat pair like [A,B] and [B,A], and neither will there be more than 2 people in a relationship.

I want to write a function that creates a dictionary of how many friends each person has. People can have 0 to many friends.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the Python code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

def friends(lstOfList):   
d = {} #empty dictionary
for lst in lstOfList: #Traverse every list inside 2d list
if len(lst)==2: #Atleast 2 friends are present in this list
if lst[0] in d: #This person is already present in dictionary
d[lst[0]]+=1 #increment friend of 1st entry by 1
else:
d[lst[0]]=1 #This person appeared first time hence initialize it by 1

if lst[1] in d: #This person is already present in dictionary
d[lst[1]]+=1 #increment friend of 1st entry by 1
else:
d[lst[1]]=1 #This person appeared first time hence initialize it by 1
else: #single entry in this list
if lst[0] not in d: #This person is not present in dictionary
d[lst[0]]=0 #This person(single) appeared first time hence initialize it by 0
return d

print(friends([['A', 'B'], ['B', 'C'], ['B', 'D'], ['E']]))

test.py- CA Windows System321test.py (3.5.0) File Edit Format Run Options Window Help friends (1st0fLǐst): d-{} #empty dictio

Below is the screenshot of output

Python 3.5.0 Shell File Edit Shell Debug Options Window Help Python 3.5.0 (v3.5.0: 374f501f4567, Sep 13 2015, 02:16:59) [MSC

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.

Add a comment
Know the answer?
Add Answer to:
Hi, I have a python coding questions and would need help: Count the neighbors of each node in a g...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • I am practicing python by doing some projects, and I am stuck on this function. I'd...

    I am practicing python by doing some projects, and I am stuck on this function. I'd like to know how to do it without using statements like break, input, print, or continue. The first parameter represents a "person to friends" dictionary, the second parameter represents a "person to clubs" dictionary, and the third parameter represents a person's name in the same format as the dictionary keys). Using the recommendation system described below, the function should return the club recommendations for...

  • In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as...

    In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as keys and an array for key containing a list of values (words from file of respective size) associated with those keys I am reading from a file of strings, where I am only interested with words of length 4, 5, and 6 to compute my program reading the text file line by line: At first, I have an empty dictionary then to that I...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard....

  • Need help coding this python problem You have three friends who want to rent a room...

    Need help coding this python problem You have three friends who want to rent a room in your house. The amount they are willing to pay you depends on the length of their stay. Bill will pay you a flat rate of $100 dollars every day. John will pay you $1000 dollars the first day and half as much each subsequent day, meaning on the first day he will pay $1000, the second day he will pay $500, on the...

  • Python 3 Coding Functions: Here is any code required to help code what is below: def...

    Python 3 Coding Functions: Here is any code required to help code what is below: def pokemon_by_types(db, types): new_db = {} for pokemon_type in types: for key in db: # iterate through all the type in types list if db[key][1] == pokemon_type or db[key][2] == pokemon_type: if key not in new_db: new_db[key] = db[key] return new_db I need help coding the functions listed below in the image: Thank you get types(db): Given a database db, this function determines all the...

  • Hi, I need some help finishing the last part of this Python 1 code. The last...

    Hi, I need some help finishing the last part of this Python 1 code. The last few functions are incomplete. Thank you. The instructions were: The program has three functions in it. I’ve written all of break_into_list_of_words()--DO NOT CHANGE THIS ONE. All it does is break the very long poem into a list of individual words. Some of what it's doing will not make much sense to you until we get to the strings chapter, and that's fine--that's part of...

  • Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...

    Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :) Here is the questions in this assignment: Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...

  • I need help writing this C code. Here is the description: Let's say we have a...

    I need help writing this C code. Here is the description: Let's say we have a program called smart typing which does two things: The first letter of a word is always input manually by a keystroke. When a sequence of n letters has been typed: c1c2...cn and the set of words in the dictionary that start with that sequence also have c1c2...cnc then the smart typing displays c automatically. we would like to know,  for each word in the dictionary,...

  • I need help with this problem. Using Python please. Thanks Construct a class “Monster” with the...

    I need help with this problem. Using Python please. Thanks Construct a class “Monster” with the following attributes: self.name (a string) self.type (a string, default is ‘Normal’) self.current_hp (int, starts out equal to max_hp) self.max_hp (int, is given as input when the class instance is created, default is 20) self.exp (int, starts at 0, is increased by fighting) self.attacks (a dict of all known attacks) self.possible_attacks (a dictionary of all possible attacks The dictionary of possible_attacks will map the name...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT