Skip to content

Instantly share code, notes, and snippets.

@raganmd
Last active December 14, 2017 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raganmd/e55a5ead6fa0f75d3f05f4435a7d5aa4 to your computer and use it in GitHub Desktop.
Save raganmd/e55a5ead6fa0f75d3f05f4435a7d5aa4 to your computer and use it in GitHub Desktop.
Let's start by doing something seemingly simple - filling out a table with the contents of our dictionary. Why put things into a table?! At some point you'll certainly want to fill up a table with the contents of a dictionary... so we might as well look at how to do this early on.
# define our variables
# using modules means that we can use our dot notation to
# access an object inside of our text DAT
inventory = mod( 'text_test_dictionary' ).inventory
table = op( 'table_inventory' )
table_length = []
place_holder = 1
# first things first, let's clear out the contents of the table
table.clear()
# loop through the dictionary and add headers
for dictionary_key , dictionary_value in inventory.items():
table.appendCol( [ dictionary_key ] )
# add a blank column for next set of data points
table.appendCol()
# add to the list so we can determine the max length of the table
table_length.append( len( dictionary_value ) )
# set the table length based on the maximum number of dictionary entries
table.setSize( max( table_length ) + 1 , table.numCols )
# loop through dictionary again to fill in table
for dictionary_key , dictionary_value in inventory.items():
# generate an enumerated list to loop through
value_list = list( enumerate( dictionary_value ) )
# use enumerated list to fill in table
for item in value_list:
table[ item[ 0 ] + 1 , dictionary_key ] = item[ 1 ]
table[ item[ 0 ] + 1 , place_holder ] = inventory[ dictionary_key ][ item[ 1 ] ]
# increment placeholder to ensure that our data goes in the right place
place_holder += 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment