2014-07-05 19 views
-3

所以我有话名单:的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", 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. 

任何帮助将不胜感激。

+2

如果你想知道关于向下票:这是你的第二个问题,你又已经证明没有力气解决问题你自己,只是要求解决方案给你。 – mklement0

回答

0

AppleScript不是这项工作的正确工具,所以虽然下面的解决方案工作,它是低调。

(为了获得更好的性能,你需要充分哈希表的功能,它的AppleScript不提供 - 的AppleScript的record类是严重不能妨碍到指定键动态,在运行时,通过变量 - 三阶尽管存在第三方解决方案(例如,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. 
+0

感谢您的回复,我非常想自己解决问题。 – user3803526

+0

@ user3803526:不客气。对于所有人的利益,我鼓励你在发布问题时表现出一些努力:你将通过试图自己解决问题来学习,如果你这样做,其他人更可能帮助你,甚至其他人也可能从中受益给出的答案。 – mklement0

0

做的另一种方式......

# 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 
+0

有趣的方法,但它解决了_different_问题:OP的所需输出是_so far_的出现次数(输出列表镜像输入列表中的单词,包括重复项); (两件事情可以很容易地纠正),你输出列表的元素不是列表,你的匹配是case-_sensitive_,而OP的输出显示case-insensitive匹配。 – mklement0

+0

好点。我没有完全阅读这个问题 – adayzdone

相关问题