Friday, January 28, 2022

#1 2021-08-08 08:06:07 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1218

Converting a List of Lists to a Dictionary

I was just working on my ASObjC notebook, and the goal was to convert a list of lists to a dictionary. I had hoped to avoid the use of a repeat loop but without success. In testing with Script Geek, the first script took 6 milliseconds and only 2 milliseconds if a script object was employed. The second script took 16 milliseconds, and the use of a script object actually increased the execution time.

The list of lists used in the tests was:

Applescript:

set theList to {{"a", "aaa"}, {"b", "bbb"}, {"c", "ccc"}, {"d", "ddd"}, {"e", "eee"}, {"f", "fff"}, {"g", "ggg"}, {"h", "hhh"}, {"i", "iii"}, {"j", "jjj"}, {"k", "kkk"}, {"l", "lll"}, {"m", "mmm"}, {"n", "nnn"}, {"o", "ooo"}, {"p", "ppp"}, {"q", "qqq"}, {"r", "rrr"}, {"s", "sss"}, {"t", "ttt"}, {"u", "uuu"}, {"v", "vvv"}, {"w", "www"}, {"x", "xxx"}, {"y", "yyy"}, {"z", "zzz"}}

Tested script one was:

Applescript:

use framework "Foundation"
-- insert theList here
set {theKeys, theValues} to {{}, {}}
repeat with aList in theList -- timing result is 6 milliseconds
   -- repeat with aList in my theList -- reduces timing result to 2 milliseconds
   set {end of theKeys, end of theValues} to {item 1 of aList, item 2 of aList}
end repeat
set theDictionary to current application's NSDictionary's dictionaryWithObjects:theValues forKeys:theKeys

Tested script two was:

Applescript:

use framework "Foundation"
-- insert theList here
set theDictionary to (current application's NSMutableDictionary's new())
repeat with aList in theList
   (theDictionary's setObject:(item 2 of aList) forKey:(item 1 of aList))
end repeat -- 16 milliseconds

A slight variant of script two was to bridge theList to an array and then to loop through the array but the timing result was about the same.

Last edited by peavine (2021-08-08 02:12:06 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#2 2021-08-08 09:34:33 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1218

Re: Converting a List of Lists to a Dictionary

Anyone having a need to convert a list of lists to a dictionary would probably want to do that in a handler, perhaps as shown below. The timing result was 2 milliseconds.

Applescript:

use framework "Foundation"

set theList to {{"a", "aaa"}, {"b", "bbb"}, {"c", "ccc"}, {"d", "ddd"}, {"e", "eee"}, {"f", "fff"}, {"g", "ggg"}, {"h", "hhh"}, {"i", "iii"}, {"j", "jjj"}, {"k", "kkk"}, {"l", "lll"}, {"m", "mmm"}, {"n", "nnn"}, {"o", "ooo"}, {"p", "ppp"}, {"q", "qqq"}, {"r", "rrr"}, {"s", "sss"}, {"t", "ttt"}, {"u", "uuu"}, {"v", "vvv"}, {"w", "www"}, {"x", "xxx"}, {"y", "yyy"}, {"z", "zzz"}}

set theDictionary to listToDictionary(a reference to theList)

on listToDictionary(theList)
   set {theKeys, theValues} to {{}, {}}
   repeat with aList in theList
       set {end of theKeys, end of theValues} to {item 1 of aList, item 2 of aList}
   end repeat
   return (current application's NSDictionary's dictionaryWithObjects:theValues forKeys:theKeys)
end listToDictionary -- 2 milliseconds


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)