Applescript:根据列表中该单词的出现次数,对包含单词的列表的每个子列表进行编号

时间:2014-07-05 20:43:19

标签: applescript

我有一个这样的清单:

 {{"269", "It"}, {"439", "was"}, {"509", "the"}, {"829", "best"}, {"1059", "of"}, {"1350", "times"}, {"1449", "it"}, {"2089", "was"}, {"2659", "the"}, {"3250", "worst"}, {"3429", "of"}, {"3529", "times"}, {"4219", "it"}, {"4519", "was"}, {"5210", "the"}, {"5629", "age"}, {"6049", "of"}, {"6279", "wisdom"}, {"6599", "it"}, {"6659", "was"}, {"6839", "the"}, {"7399", "age"}, {"of"}, {"7639", "foolishness"}, {"8019", "it"}, {"8619", "was"}, {"9369", "the"}, {"9477", "epoch"}, {"9682", "of"}, {"9897", "belief"}}

我希望它也有序列中每个单词出现的数字:

{{"269", "It", 1}, {"439", "was", 1}, {"509", "the", 1}, {"829", "best", 1}, {"1059", "of", 1}, {"1350", "times", 1}, {"1449", "it", 2}, {"2089", "was", 2}, {"2659", "the", 2}, {"3250", "worst", 1}, {"3429", "of", 2}, {"3529", "times", 2}, {"4219", "it", 3}, {"4519", "was", 3}, {"5210", "the", 3}, {"5629", "age", 1}, {"6049", "of", 3}, {"6279", "wisdom", 1}, {"6599", "it", 4}, {"6659", "was", 4}, {"6839", "the", 4}, {"7399", "age", 2}, {"of" 4}, {"7639", "foolishness", 1}, {"8019", "it", 4}, {"8619", "was", 5}, {"9369", "the", 5}, {"9477", "epoch", 1}, {"9682", "of", 5}, {"9897", "belief", 1}}

我试图修改脚本mklement0给我这样的:

set inList to {{"269", "It"}, {"439", "was"}, {"509", "the"}, {"829", "best"}, {"1059", "of"}, {"1350", "times"}, {"1449", "it"}, {"2089", "was"}, {"2659", "the"}, {"3250", "worst"}, {"3429", "of"}, {"3529", "times"}, {"4219", "it"}, {"4519", "was"}, {"5210", "the"}, {"5629", "age"}, {"6049", "of"}, {"6279", "wisdom"}, {"6599", "it"}, {"6659", "was"}, {"6839", "the"}, {"7399", "age"}, {"of"}, {"7639", "foolishness"}, {"8019", "it"}, {"8619", "was"}, {"9369", "the"}, {"9477", "epoch"}, {"9682", "of"}, {"9897", "belief"}}
set word_list to {}
repeat with i from 1 to (length of inList)
    set the_word to item i of inList
    if item 2 of the_word is word then
        set outList to outList & {{contents of the_word, 1 + (my countOccurrences(contents of inList, word_list))}}
    end if
end repeat

它给我一个错误:     “无法获得{\”的第2项。“编号为-1728,来自{“of”}

的第2项

显然,我甚至不知道从哪里开始。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

试试这个

set inList to {{"269", "It"}, {"439", "was"}, {"509", "the"}, {"829", "best"}, {"1059", "of"}, {"1350", "times"}, {"1449", "it"}, {"2089", "was"}, {"2659", "the"}, {"3250", "worst"}, {"3429", "of"}, {"3529", "times"}, {"4219", "it"}, {"4519", "was"}, {"5210", "the"}, {"5629", "age"}, {"6049", "of"}, {"6279", "wisdom"}, {"6599", "it"}, {"6659", "was"}, {"6839", "the"}, {"7399", "age"}, {"7639", "foolishness"}, {"8019", "it"}, {"8619", "was"}, {"9369", "the"}, {"9477", "epoch"}, {"9682", "of"}, {"9897", "belief"}}

my occurencInSubList(inList, 2) -- first param : a list of subLists, second param : index of the word in each subList
inList -- the results are in inList.

on occurencInSubList(L, n)
    set dupeList to {}
    set tc to length of L -- numbers of subList
    repeat with i from 1 to tc
        set the_word to item n of list i of L -- get the second item in this sublist
        if the_word is not in dupeList then -- to not counting the occurrences of the same word again.
            set end of dupeList to the_word
            set cnt to 1
            repeat with j from i to tc -- count the occurrences of this word once
                tell list j of L to if item n = the_word then
                    set end to cnt --- add an item (the count ) at the end of this subList
                    set cnt to cnt + 1
                end if
            end repeat
        end if
    end repeat
end occurencInSubList

答案 1 :(得分:0)

这是一个适合我的previous answer

新输入列表格式的版本

注意:正如@a​​damh在评论中指出的那样,您的输入列表格式不正确,因为它缺少"of"个子列表中的一个的第一个子列表项 - 我只是插入了{{1}在我的下面的代码中。

"0"