Need to refer to Value of a key of a dictionary nested within a dictionary - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need to refer to Value of a key of a dictionary nested within a dictionary (/thread-236.html) Pages:
1
2
|
Need to refer to Value of a key of a dictionary nested within a dictionary - xepicxmonkeyx - Oct-01-2016 You can find my code at the link below, and you can ignore any commented sections of code (Accept the one in make_grid() ): link removed, code added below from __future__ import print_function from random import randint list = { 11: {'x': 1, 'y': 1, 'onScreen': 'X', 'occupied': False}, 12: {'x': 1, 'y': 2, 'onScreen': 'X', 'occupied': False}, 13: {'x': 1, 'y': 3, 'onScreen': 'O', 'occupied': True}, 14: {'x': 1, 'y': 4, 'onScreen': 'O', 'occupied': True}, 15: {'x': 1, 'y': 5, 'onScreen': 'O', 'occupied': True}, 16: {'x': 1, 'y': 6, 'onScreen': 'X', 'occupied': False}, 17: {'x': 1, 'y': 7, 'onScreen': 'X', 'occupied': False}, 21: {'x': 2, 'y': 1, 'onScreen': 'X', 'occupied': False}, 22: {'x': 2, 'y': 2, 'onScreen': 'X', 'occupied': False}, 23: {'x': 2, 'y': 3, 'onScreen': 'O', 'occupied': True}, 24: {'x': 2, 'y': 4, 'onScreen': 'O', 'occupied': True}, 25: {'x': 2, 'y': 5, 'onScreen': 'O', 'occupied': True}, 26: {'x': 2, 'y': 6, 'onScreen': 'X', 'occupied': False}, 27: {'x': 2, 'y': 7, 'onScreen': 'X', 'occupied': False}, 31: {'x': 3, 'y': 1, 'onScreen': 'X', 'occupied': True}, 32: {'x': 3, 'y': 2, 'onScreen': 'O', 'occupied': True}, 33: {'x': 3, 'y': 3, 'onScreen': 'O', 'occupied': True}, 34: {'x': 3, 'y': 4, 'onScreen': 'O', 'occupied': True}, 35: {'x': 3, 'y': 5, 'onScreen': 'O', 'occupied': True}, 36: {'x': 3, 'y': 6, 'onScreen': 'O', 'occupied': True}, 37: {'x': 3, 'y': 7, 'onScreen': 'O', 'occupied': True}, 41: {'x': 4, 'y': 1, 'onScreen': 'O', 'occupied': True}, 42: {'x': 4, 'y': 2, 'onScreen': 'O', 'occupied': True}, 43: {'x': 4, 'y': 3, 'onScreen': 'O', 'occupied': True}, 44: {'x': 4, 'y': 4, 'onScreen': 'O', 'occupied': True}, 45: {'x': 4, 'y': 5, 'onScreen': 'O', 'occupied': True}, 46: {'x': 4, 'y': 6, 'onScreen': 'O', 'occupied': True}, 47: {'x': 4, 'y': 7, 'onScreen': 'O', 'occupied': True}, 51: {'x': 5, 'y': 1, 'onScreen': 'O', 'occupied': True}, 52: {'x': 5, 'y': 2, 'onScreen': 'O', 'occupied': True}, 53: {'x': 5, 'y': 3, 'onScreen': 'O', 'occupied': True}, 54: {'x': 5, 'y': 4, 'onScreen': 'O', 'occupied': True}, 55: {'x': 5, 'y': 5, 'onScreen': 'O', 'occupied': True}, 56: {'x': 5, 'y': 6, 'onScreen': 'O', 'occupied': True}, 57: {'x': 5, 'y': 7, 'onScreen': 'O', 'occupied': True}, 61: {'x': 6, 'y': 1, 'onScreen': 'O', 'occupied': False}, 62: {'x': 6, 'y': 2, 'onScreen': 'X', 'occupied': False}, 63: {'x': 6, 'y': 3, 'onScreen': 'X', 'occupied': True}, 64: {'x': 6, 'y': 4, 'onScreen': 'O', 'occupied': True}, 65: {'x': 6, 'y': 5, 'onScreen': 'O', 'occupied': True}, 66: {'x': 6, 'y': 6, 'onScreen': 'O', 'occupied': False}, 67: {'x': 6, 'y': 7, 'onScreen': 'X', 'occupied': False}, 71: {'x': 7, 'y': 1, 'onScreen': 'X', 'occupied': False}, 72: {'x': 7, 'y': 2, 'onScreen': 'X', 'occupied': False}, 73: {'x': 7, 'y': 3, 'onScreen': 'X', 'occupied': True}, 74: {'x': 7, 'y': 4, 'onScreen': 'O', 'occupied': True}, 75: {'x': 7, 'y': 5, 'onScreen': 'O', 'occupied': True}, 76: {'x': 7, 'y': 6, 'onScreen': 'O', 'occupied': False}, 77: {'x': 7, 'y': 7, 'onScreen': 'O', 'occupied': False}, } # Sends data class readGrid(object): def __init__(self, x, y, boolean, occu): self.x = x self.y = y self.boolean = boolean # produces the Peg Grid, and outputs it grid_list = [] list def make_grid(): for key, item in list[11]: print(item) #my_var = readGrid(sub_list[0], sub_list[1], sub_list[2], sub_list[3]) #grid_list.append(my_var.boolean) #print(*grid_list, sep=', ') make_grid() #for item in list: # if item[11]['onScreen']: # # twoOverKey = str(item['x'] + 2) + str(item['y']); # twoOver = list[twoOverKey]; # if twoOver['occupied'] == False: # print("h") # do somethingWhat I'm trying to do is print out the Character held in 'onScreen' by for-looping through the dictionary in the make_grid() function. Of course this would be easy if it was a list, but... it's not! RE: Need to refer to Value of a key of a dictionary nested within a dictionary - metulburr - Oct-02-2016 Quote:What I'm trying to do is print out the Character held in 'onScreen' by for-looping through the dictionary in the make_grid() function. Of course this would be easy if it was a list, but... it's not!You can loop through a dictionary too for k,v in list.items(): print(v['onScreen'])You also do not need to import future print on 2.7.10 and you should not overwrite "list" built-in https://repl.it/X3G/4836 RE: Need to refer to Value of a key of a dictionary nested within a dictionary - xepicxmonkeyx - Oct-02-2016 (Oct-02-2016, 01:00 AM)metulburr Wrote:Quote:What I'm trying to do is print out the Character held in 'onScreen' by for-looping through the dictionary in the make_grid() function. Of course this would be easy if it was a list, but... it's not!You can loop through a dictionary too This worked like a charm, had it printing out randomly at the start (don't know why), but got it working in the end! RE: Need to refer to Value of a key of a dictionary nested within a dictionary - Skaperen - Oct-02-2016 (Oct-02-2016, 01:00 AM)metulburr Wrote: You also do not need to import future print on 2.7.10 but if you are giving away (or selling) code for others to just run, it might still be needed for a system that has 2.6 :( RE: Need to refer to Value of a key of a dictionary nested within a dictionary - metulburr - Oct-02-2016 (Oct-02-2016, 05:40 AM)Skaperen Wrote:(Oct-02-2016, 01:00 AM)metulburr Wrote: You also do not need to import future print on 2.7.10 Thats why i specifically said 2.7.10. 2.6 is quite old. And if you are giving away or selling code for others to run you should build an executable with it so that they dont even need python. Then you can attach whichever python interpreter your program needs with it. RE: Need to refer to Value of a key of a dictionary nested within a dictionary - xepicxmonkeyx - Oct-02-2016 (Oct-02-2016, 02:26 AM)xepicxmonkeyx Wrote:(Oct-02-2016, 01:00 AM)metulburr Wrote:Quote:What I'm trying to do is print out the Character held in 'onScreen' by for-looping through the dictionary in the make_grid() function. Of course this would be easy if it was a list, but... it's not!You can loop through a dictionary too I mentioned it printed out the dict randomly. For the printing of the grid that wasn't something I needed. But how do I make it do so when I do? RE: Need to refer to Value of a key of a dictionary nested within a dictionary - Yoriz - Oct-02-2016 dict have no order(unless using ordered dict), so will be printed randomly, to print the values in an order the key will have to be used. A list will always print in the same order. RE: Need to refer to Value of a key of a dictionary nested within a dictionary - metulburr - Oct-02-2016 You can use collections.OrderdDict to maintain the order https://pymotw.com/2/collections/ordereddict.html RE: Need to refer to Value of a key of a dictionary nested within a dictionary - wavic - Oct-02-2016 In version 3.6 dicts will be ordered RE: Need to refer to Value of a key of a dictionary nested within a dictionary - j.crater - Oct-02-2016 (Oct-02-2016, 03:42 PM)wavic Wrote: In version 3.6 dicts will be ordered Oh my, that's some news! Sorry for being ignorant and not up to date with news... do you have a source for this info? |