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
  Pass Dictionary to Method is not possible Frank123456 1 78 Aug-19-2019, 10:18 AM
Last Post: buran
  Dictionary for Excel Data Ranjirock 0 83 Aug-18-2019, 05:26 PM
Last Post: Ranjirock
  Basic one: Aggregating from a dictionary Mustey 1 125 Aug-12-2019, 07:33 PM
Last Post: buran
  Parsing json - dictionary with in dictionary krish216 6 292 Jul-30-2019, 10:01 PM
Last Post: cvsae
  Create XML from dictionary pygrrrl 7 252 Jul-29-2019, 08:33 PM
Last Post: pygrrrl
  Dictionary help Tridium 3 116 Jul-29-2019, 09:41 AM
Last Post: Tridium
  How do you import a dictionary from a .csv file? SteampunkMaverick12 2 156 Jul-29-2019, 09:20 AM
Last Post: sudheerm
  nested dictionaries to CSV mart79 9 369 Jul-29-2019, 04:59 AM
Last Post: mart79
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 135 Jul-25-2019, 11:32 PM
Last Post: scidam
  Need help with dictionary! hoangthai10788 3 117 Jul-18-2019, 08:55 AM
Last Post: DeaD_EyE

Forum Jump:


Users browsing this thread: 1 Guest(s)