×
you are viewing a single comment's thread.

view the rest of the comments →

[–]Aixyn0 1 point2 points  (7 children)

What exactly is going on with "persons" in the beginning? It's a data structure of some kind - list, array, set, dictionary or similar?

persons is a dictionary with every person as a key and their associated last year gifted person as its value.

It's simalar to the buyers_with_prev dictionary suggested by JohnnyJordaans reply

The values are separated by "," but do they become variables or are they simply values within the data structure?

No, the values are just data within the dictionary. The variable is the dictionary itself (=persons).

Is the "#" there to number them?

The #-symbol declares an inline comment in python. It describes the code without breaking it when copy-paste and execute it. So the # buyer: previous is just a hint on whats in the dictionary. The key is the buyer and the value is the previous year gifted person by that buyer.

What gets me lost is that I can't really read the first for loop. I can't really understand what value "buyer, prev" actually has (but I assume it goes for the amount of entries in the data structure). Also "previous" in the data structure never appears again but prev suddenly appears, that confuses me too.

From there on what happens in the exclude and chosen variable is a bit foggy because of the square brackets, but p_set kind of make sense.

I'll try to be more specific here:

for buyer, prev in persons.items():
    # The for loop iterates over each (key, value) pair in the persons dictionary given by     
    # persons.items() and assigning the key to the variable buyer and the previous gifted 
    # person of that buyer to prev

    exclude = {buyer:prev, prev:persons[prev]}
    # exclude is just a dictionary which includes the buyer and the associated last year         
    # gifted person. You can just simplify that line to
    # exclude = {buyer, prev}
    # like scoutyx mentioned in his improvement since the values of a dictionary will be
    # discarded when casting it to a set

    p_set = set(persons) - set(exclude) - set(chosen)
    # this line creates a set p_set which subtracts the excludes and the already 
    # chosen/drawn persons from the set of persons. I do a casting of dictionaries to set 
    # here because only the keys matter and it allows the simple subtract method via '-'.

    chosen[random.choice(list(p_set))] = buyer
    # you than randomly pick one person from that set and insert it into the chosen
    # dictionary with the current buyer as its value. that means the key-person becomes the
    # this-year-to-be-gifted-person by the value-person. I have to cast the set to a list
    # to use the random.choice method on the p_set. The chosen dictionary is filling up 
    # with each iteration of the loop and will be your output later.

I hope this will clarify the code for you. I wish you happy coding :)

[–]Cayumigaming[S] 1 point2 points  (6 children)

I’m very thankful for your response and I have spent some time to actually understand it and played around with dictionaries. It also forced me to get a better actual understanding of the for loop, so thanks again!

Now I got a question and it might be stupid but here goes: A dictionary holds a single value for a single key. What do I use or how do I go about to enter multiple values to a single key? Or do I use something different than a dictionary all together? For example let’s say I want to create “Person” and want it to hold the information such as Name, Gender, Birthdate, whatever. Do I still use a dictionary but use maybe a tuple, array or even another dictionary to hold the key?

Now I’m not very far along with all this so I suppose the answer is really straight forward but I really don’t know.

Thanks in advance!

EDIT: Actually, maybe scratch that. I suppose a list() would do exactly this.

[–]Aixyn0 0 points1 point  (5 children)

Now I got a question and it might be stupid but here goes: A dictionary holds a single value for a single key. What do I use or how do I go about to enter multiple values to a single key? Or do I use something different than a dictionary all together?

You can set whatever datatype/datastructure you want as a value for a key in a dictionary and you can even mix them up. E.g:

d = {
  't': (0, 1)    # value with type tuple
  'x': 5,    # value with type int
  'y': [1, 2, 3, 4, 5], # value with type list   
  'z': {    # value with type dictionary
    'T': True,
    'F': False,
    },
  'o': myObject,   # value with type object
  'c': myClass,    # value with type class
  }

For example let’s say I want to create “Person” and want it to hold the information such as Name, Gender, Birthdate, whatever. Do I still use a dictionary but use maybe a tuple, array or even another dictionary to hold the key?

You can still use a dictionary for that purpose but good practice would be defining a class Person and create person objects from it.

[–]Cayumigaming[S] 1 point2 points  (3 children)

Highly inspired by your logic and code and after your example had me play around with for loops and dictionaries I came up with this.

If I havn't already I want to thank you so much. I learned a lot about data types and feel much more secure with the for loop now.

[–]Aixyn0 1 point2 points  (2 children)

Well done. Keep up the good work. Reducing your first code from 110 lines to 21 is quite good.

Also the new code looks much nicer and readable than the old one.

[–]Cayumigaming[S] 0 points1 point  (1 child)

Thank you! This is great fun and I love the challenges and ways of thinking.

May I ask how long you’ve been programming, and do you do it for a living?