Make dictionarys indexable

Request a new command, feature, or improvement

Moderator: MSandro

Post Reply
SimpleSi
Posts: 326
Joined: Jul 2nd, '17, 13:47

Make dictionarys indexable

Post by SimpleSi » Sep 25th, '17, 14:16

I'd like to be able to access items in a dictionary by index as well as keyname so I can loop thru looking for particular data

preferably something like

for i in dictionaryVariable

where i gets assigned the key for each key:item pair in the dictionary

or

where i is assinged as a 2 element list containing the key and data

Current workaround is to just use two synchronised lists instead of one dictionary

Simon

SimpleSi
Posts: 326
Joined: Jul 2nd, '17, 13:47

Re: Make dictionarys indexable

Post by SimpleSi » Sep 25th, '17, 14:47

Changed workaround to use single list with each item in the list be another list with 2 elements (key/pair data)

SimpleSi
Posts: 326
Joined: Jul 2nd, '17, 13:47

Re: Make dictionarys indexable

Post by SimpleSi » Oct 1st, '17, 10:45

Looking through system palette led me to this method of looping thru
dictiter.png
dictiter.png (9.67 KiB) Viewed 1170 times

JohnM
Posts: 359
Joined: Sep 11th, '15, 14:42

Re: Make dictionarys indexable

Post by JohnM » Oct 31st, '17, 15:10

Yes, iterating over the keys of a dictionary is a good way to iterate over all elements. The keys are just a list, which you can sort if you want to access the elements in key-sorted order.

Another useful dictionary method is "sortedPairs", which returns an array (which, as you know, is similar to a list except you can't change it's length) of two-element arrays, either (key, value) pairs sorted by key (if the second argument is true) or (value, key) pairs sorted by value.

This method isn't in the palette because it's a little complicated to explain, but it can be very useful for collecting statistics. For example, you can put every word in the the text of a book from www.gutenberg.org, such as "Twenty Thousand Leagues Under the Sea" by Jules Verne, and then use sortedPairs to find out which words are the most common:
Screen Shot 2017-10-31 at 11.06.25 AM.png
which gives the result:
Screen Shot 2017-10-31 at 11.06.06 AM.png
Not surprisingly, the word "the" is the most common, but "Captain" and "Nemo" also appear often in that particular book.

SimpleSi
Posts: 326
Joined: Jul 2nd, '17, 13:47

Re: Make dictionarys indexable

Post by SimpleSi » Oct 31st, '17, 16:12

I was a bit confused by the

to d add word

so I wrote
scriptImage8.png
scriptImage8.png (4.96 KiB) Viewed 1134 times
and then examined current and got

(dictionary 3 1 6 1 9 1 1 1 4 1 7 1 10 1 2 1 5 1 8 1)

then ran
scriptImage9.png
scriptImage9.png (3.27 KiB) Viewed 1134 times
and got
(dictionary 3 2 6 2 9 2 1 2 4 2 7 2 10 2 2 2 5 2 8 2)

So it looks like adding an item to a dictionary without specifying a key/value pair gets it to simply create a a key (if it doesn't exist) or add 1 to the count of the key (if it does already exist)

Now, thats an impressive facility but not intuitive at all

I've not used dictionaries much (just a little in Python and javascript - is this standard behaviour or a GP special?

If its not general dictionary behaviour then should it be in GP?

JohnM
Posts: 359
Joined: Sep 11th, '15, 14:42

Re: Make dictionarys indexable

Post by JohnM » Oct 31st, '17, 17:39

So it looks like adding an item to a dictionary without specifying a key/value pair gets it to simply create a a key (if it doesn't exist) or add 1 to the count of the key (if it does already exist)
Yes, precisely!

The libraries of other languages often use a different class, such as "Bag" or "MultiSet", to implement key counting behavior. Yet when you look more closely, "MultiSet" and "Dictionary" are usually 90% the same in both functionality and implementation. In languages with many data structures to choose frome, it can be difficult for beginners to figure out exactly which class to use for a given application. GP strives to make such decisions easier to by reducing the number of variations. So, in GP, "lists" are used for ordered collections of items and dictionaries are used for name-value pairs. (Lua takes this philosophy even further, combining the functionality of lists, dictionaries, arrays, and objects all into a single concept.)
If its not general dictionary behaviour then should it be in GP?
I don't see why not. Not much in is GP is *exactly* like other programming languages, but I hope that the concepts in GP (such as classes and instances, lists, dictionaries, etc.) are similar enough to their equivalents to make it easy apply what one learns in GP to other languages. For example, GP's objects are not exactly the same as Java's, but at least one AP CS teacher has found GP to be a good "warm up" to teaching Java. Or to take this particular example, in Python you could still use a dictionary to compute word count statistics, but you'd increment a word count with something like:

Code: Select all

dict[word] = dict.get(word, default=0) + 1
(That may not be the exact Python syntax, but you get the idea.) So, someone who has seen the word counting program in GP will be able to apply the idea in Python. They just need to check the Python dictionary API to figure out the Python equivalent of GP's elegant shortcut.

SimpleSi
Posts: 326
Joined: Jul 2nd, '17, 13:47

Re: Make dictionarys indexable

Post by SimpleSi » Oct 31st, '17, 19:24

elegant shortcut
:)

OK :)

Any other "elegant shortcuts" that you can remember sneaking in? :)

Post Reply