AppleScript的:根据它们的序列中发生的话号码列表 [英] Applescript: number list of words according to their occurrence in the sequence
问题描述
所以,我有话名单:
{"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"}
我想一个字的每次出现,以根据它已经发生的序列中的次数进行编号:
I'd like each occurrence of a word to be numbered according to the number of times it has occurred in the sequence:
{"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.
任何帮助将大大AP preciated。
Any help would be greatly appreciated.
推荐答案
AppleScript的不是这个工作的工具,因此,尽管下面的解决方案有效,这是sloooow。
AppleScript is not the right tool for this job, so while the following solution works, it is sloooow.
(为了获得更好的性能,你需要完整的哈希表的功能,它的AppleScript不提供 - 的AppleScript的记录
类受到严重无法指定键阻碍动态的,在运行时,通过变量 - 第三方解决方案存在,但(例如,的 http://www.latenightsw.com/freeware/list-record-tools/ ))。
(For better performance, you'd need full hash-table functionality, which AppleScript doesn't provide - AppleScript's record
class is severely handicapped by the inability to specify keys dynamically, at runtime, via variables - third-party solutions exist, though (e.g., http://www.latenightsw.com/freeware/list-record-tools/)).
# Helper handler: Given a search word, returns the number of times the word
# already occurs in the specified list (as the first sub-item of each list item).
on countOccurrences(searchWrd, lst)
local counter
set counter to 0
repeat with wordNumPair in lst
if item 1 of wordNumPair is searchWrd then
set counter to counter + 1
end if
end repeat
return counter
end countOccurrences
# 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"}
# Initialize the output list.
set outList to {}
# Loop over the input list and build the output list incrementally.
repeat with wrd in inList
# Note that `contents of` returns the string content of the list item
# (dereferences the object specifier that `repeat with` returns).
set outList to outList & {{contents of wrd, 1 + (my countOccurrences(contents of wrd, outList))}}
end repeat
# outList now contains the desired result.
这篇关于AppleScript的:根据它们的序列中发生的话号码列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!