Applescript:使用一个列表清除另一个列表

时间:2014-07-03 23:11:47

标签: applescript

所以我有这个单词列表:

{{"YOU"}, {"KNOW"}, {"BETTER"}, {"THAN"}, {"TO"}, {"PARK"}, {"YOUR"}, {"CAR"}, {"IN"}, {"UNASSIGNED"}, {"SPOTS"}}

我也有这个清单:

{{"0", "<SIL>"}, {"269", "YOU"}, {"389", "<SIL>"}, {"439", "KNOW"}, {"509", "BETTER"}, {"829", "THAN"}, {"969", "TO"}, {"1059", "PARK"}, {"1350", "YOUR"}, {"1449", "CAR"}, {"1839", "IN"}, {"2059", "<SIL>"}, {"2089", "UNASSIGNED"}, {"2659", "SPOTS"}}

我想清除所有无关的子列表(那些不包含第一个列表中的单词的子列表),留下这样的内容:

{{"269", "YOU"}, {"439", "KNOW"}, {"509", "BETTER"}, {"829", "THAN"}, {"969", "TO"}, {"1059", "PARK"}, {"1350", "YOUR"}, {"1449", "CAR"}, {"1839", "IN"}, {"2089", "UNASSIGNED"}, {"2659", "SPOTS"}}

我是Apple的新手,所以这对我来说真的很难。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

此脚本通过列表对一次循环一个,并且对于每个脚本,检查原始列表中的每个单词以查看它是否在该对中,如果是,则将该对添加到结果对中列表。

set L1 to {{"YOU"}, {"KNOW"}, {"BETTER"}, {"THAN"}, {"TO"}, {"PARK"}, {"YOUR"}, {"CAR"}, {"IN"}, {"UNASSIGNED"}, {"SPOTS"}}

set L2 to {{"0", "<SIL>"}, {"269", "YOU"}, {"389", "<SIL>"}, {"439", "KNOW"}, {"509", "BETTER"}, {"829", "THAN"}, {"969", "TO"}, {"1059", "PARK"}, {"1350", "YOUR"}, {"1449", "CAR"}, {"1839", "IN"}, {"2059", "<SIL>"}, {"2089", "UNASSIGNED"}, {"2659", "SPOTS"}}

set L3 to {} -- initiate the resulting list
repeat with wordPair in L2 -- cycle through each list pair in L2
    repeat with wordSingle in L1 -- check each source word to see if found in the list pair
        if wordSingle is in wordPair then
            copy (wordPair as list) to end of L3
            exit repeat
        end if
    end repeat
end repeat
return L3