0

I'd really appreciate your help. I've been trying for a while and can't figure out how to programattically create a list where you start with one level in a list of people - MY friends- such as

graph["me"] = ["alison", "bob", "candy", "duane", "eric"]

Now everyone in that first level - Alison, Bob, through eric - has 5 unique friends each (2nd level), which is portrayed as:

graph["alison"] = ["ana", "ann", "ali" "andy", "arda"]
graph["bob"] = ["bill", "bray", "bran", "brad", "bam"]

etc.

now each of those - ana, ann, bill, bray etc. each have 5 friends each (3rd level) ... up to the 5th unique level. Once we reach the fifth level, I'll assume there are no more friends.

So really, I think what I want at the end is 5 ^ 5 unique lists that start with graph["xxx"]

I'm not actually worried about the name generation - we can make the names abc1, abc2, etc. I just want it to follow the rules and go 5 x 5 levels deep, following the pattern above.

Does that make sense? :) And yes, this is related to breadth-first search. Thanks so much.

Relevant github link: https://github.com/egonSchiele/grokking_algorithms/blob/master/06_breadth-first_search/python/01_breadth-first_search.py

dbs5
  • 133
  • 2
  • 9
  • What is your actual question? Are you trying to generate these lists programmatically? If so, from what (a list of supplied data)? If not, why not just type them out in the source code, like you've done for those first three? – CrazyChucky Aug 01 '20 at 03:13

1 Answers1

0

Try this:

In [8]: me = ["alison", "bob", "candy", "duane", "eric"]

In [9]: graph = {name: [f"{name}_{i}" 
                        for i in range(5)] 
                 for name in me}

In [10]: graph['me'] = me

In [11]: graph
Out[11]: 
{'alison': ['alison_0', 'alison_1', 'alison_2', 'alison_3', 'alison_4'],
 'bob': ['bob_0', 'bob_1', 'bob_2', 'bob_3', 'bob_4'],
 'candy': ['candy_0', 'candy_1', 'candy_2', 'candy_3', 'candy_4'],
 'duane': ['duane_0', 'duane_1', 'duane_2', 'duane_3', 'duane_4'],
 'eric': ['eric_0', 'eric_1', 'eric_2', 'eric_3', 'eric_4'],
 'me': ['alison', 'bob', 'candy', 'duane', 'eric']}

In [12]: 
Paddy3118
  • 4,704
  • 27
  • 38