Applescript:根据单词在序列中的出现情况对单词列表进行编号

Applescript:根据单词在序列中的出现情况对单词列表进行编号,applescript,Applescript,所以我有一个单词列表: {"It", "was", "the", "best", "of", "times", "it", "was", "the", "worst", "of", "times", "it", "was", "the", "age", "of", "wisdom", "it", "was", "the", "age", "of", "foolishness", "it", "was", "the", "epoch", "of", "belief"} 我希望根据单词在序列中出现

所以我有一个单词列表:

{"It", "was", "the", "best", "of", "times", "it", "was", "the", "worst", "of", "times", "it", "was", "the", "age", "of", "wisdom", "it", "was", "the", "age", "of", "foolishness", "it", "was", "the", "epoch", "of", "belief"}
我希望根据单词在序列中出现的次数对每个单词进行编号:

{"It", 1}, {"was", 1}, {"the", 1}, {"best", 1}, {"of", 1}, {"times", 1} {"it", 2}, {"was", 2}, {"the", 2}, {"worst", 1}, {"of", 2}, {"times", 2}, {"it", 3}, {"was", 3}, {"the", 3}, {"age", 1}, {"of", 3}, {"wisdom", 1}, {"it", 4}, {"was", 4}, {"the", 4}, etc.

任何帮助都将不胜感激

AppleScript不是此项工作的合适工具,因此,尽管以下解决方案有效,但它是Slooow

(为了获得更好的性能,您需要完整的哈希表功能,而AppleScript没有提供这一功能-AppleScript的
record
类由于无法在运行时通过变量动态指定键而受到严重阻碍-尽管存在第三方解决方案(例如)

#Helper handler:给定一个搜索词,返回该词的次数
#已出现在指定列表中(作为每个列表项的第一个子项)。
在countOccurrences上(searchWrd、lst)
本地计数器
将计数器设置为0
用lst中的单词numpair重复
如果wordNumPair的第1项为searchWrd,则
将计数器设置为计数器+1
如果结束
结束重复
返回计数器
结束计数
#定义输入列表。
在列表中设置为{“It”、“was”、“the”、“best”、“of”、“times”、“It”、“was”、“the”、“of”、“times”、“It”、“was”、“the”、“age”、“of”、“widge”、“It”、“was”、“the”、“the”、“age”、“of”、“of”、“of”、“愚昧”、“It”、“was”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“the”、“
#初始化输出列表。
将outList设置为{}
#循环输入列表并以增量方式构建输出列表。
用inList中的wrd重复
#注意,`contents of`返回列表项的字符串内容
#(取消引用“repeat with”返回的对象说明符)。
将outList设置为outList&{{wrd的内容,1+(我的countoccurrents(wrd的内容,outList))}
结束重复
#大纲视图现在包含所需的结果。

另一种方法

# Define the input list.
set inList to {"It", "was", "the", "best", "of", "times", "it", "was", "the", "worst", "of", "times", "it", "was", "the", "age", "of", "wisdom", "it", "was", "the", "age", "of", "foolishness", "it", "was", "the", "epoch", "of", "belief"}
set AppleScript's text item delimiters to linefeed
set inListLF to inList as text
set AppleScript's text item delimiters to {""}

set usedList to {}
set resultList to {}
repeat with aWord in inList
    set aWord to contents of aWord
    considering case
        if aWord is not in usedList then
            set end of usedList to aWord
            set end of resultList to aWord & " - " & (do shell script "echo " & quoted form of inListLF & " | grep -o " & quoted form of aWord & " | wc -l")
        end if
    end considering
end repeat

set AppleScript's text item delimiters to linefeed
set resultList to resultList as text
set AppleScript's text item delimiters to {""}
return resultList

如果您对否决票感到疑惑:这是您的第二个问题,您自己也没有表现出解决问题的努力,只是要求将解决方案交给您。谢谢您的回答,我也感谢您没有亲自尝试解决问题。@user3803526:不客气。为了每个人的利益,我鼓励你在发布问题时表现出一些努力:你将通过尝试自己解决问题来学习,如果你这样做,其他人更有可能帮助你,反过来,甚至其他人也可能从给出的答案中受益。有趣的方法,但它解决了一个不同的问题:OP的期望输出是到目前为止发生的持续计数(输出列表逐字镜像输入列表,包括重复);另外(有两件事很容易纠正),输出列表的元素不是列表,匹配区分大小写,而OP的输出显示不区分大小写的匹配。