Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Need to refer to Value of a key of a dictionary nested within a dictionary
#1
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 something
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!
Quote
#2
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
Quote
#3
(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

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

This worked like a charm, had it printing out randomly at the start (don't know why), but got it working in the end!
Quote
#4
(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 :(
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#5
(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

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 :(

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.
Quote
#6
(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

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

This worked like a charm, had it printing out randomly at the start (don't know why), but got it working in the end!

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?
Quote
#7
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.
Quote
#8
You can use collections.OrderdDict to maintain the order
https://pymotw.com/2/collections/ordereddict.html
Quote
#9
In version 3.6 dicts will be ordered
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#10
(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?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Dictionary in Python kartheek 3 152 Dec-07-2019, 01:03 PM
Last Post: snippsat
  Is it possible to check for a key error in a python dictionary? mrsenorchuck 0 88 Dec-06-2019, 11:38 PM
Last Post: mrsenorchuck
  Unpacking dictionary from .xlsx as Kwargs etjkai 4 183 Dec-05-2019, 11:20 PM
Last Post: micseydel
  adding information to a dictionary. Justice_py 2 169 Dec-05-2019, 04:29 AM
Last Post: Justice_py
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 192 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck
  Dictionary and tuples list comprehensions help paul41 2 158 Nov-29-2019, 06:59 PM
Last Post: perfringo
  Assert that dictionary contains a key phpMick 3 78 Nov-27-2019, 11:36 AM
Last Post: buran
  Calculating frequency of items in a dictionary markellefultz20 1 84 Nov-27-2019, 04:21 AM
Last Post: scidam
  Read csv file, parse data, and store in a dictionary markellefultz20 4 233 Nov-26-2019, 03:33 PM
Last Post: DeaD_EyE
  [split] Print JSON Dictionary to Excel? venukommu 1 171 Nov-15-2019, 09:33 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)