【问题标题】:AppleScript - Comparing a list for works that contains any part of another listAppleScript - 比较包含另一个列表的任何部分的作品列表
【发布时间】:2013-01-31 00:15:10
【问题描述】:

输入:

set firstList to {'red ball','blue','yellow'}
set secondList to {'grasshopper','yellowjacket','blueberry','redball'}

输出:

{'yellowjacket','blueberry','redball'}

完成这样的事情的最佳方法是什么?基本上我正在寻找每个列表中的项目之间的任何相似之处。

【问题讨论】:

    标签: list applescript compare


    【解决方案1】:

    试试:

    set matchList to {}
    set firstList to {"red ball ", "blue", "yellow"}
    set secondList to {"grasshopper", "yellowjacket", "blueberry", "redball"}
    
    repeat with aItem in firstList
        set myTerm to do shell script "echo " & quoted form of aItem & " | sed -E 's/  */ ?/g'"
        repeat with bItem in secondList
            try
                set end of matchList to (do shell script "echo " & quoted form of bItem & " | grep -E " & quoted form of myTerm)
            end try
        end repeat
    end repeat
    
    return matchList
    

    编辑

        set matchList to {}
    set firstList to {"jamesdoe","jackknow","henryrod"}
    set secondList to {"James Doe", "Jack Know", "John Matthews"}
    
    set {TID, text item delimiters} to {text item delimiters, {""}}
    repeat with aItem in firstList
        set AppleScript's text item delimiters to {""}
        set myTerm to text items of aItem
        set AppleScript's text item delimiters to {" ?"}
        set myTerm to myTerm as text
        repeat with bItem in secondList
            try
                set end of matchList to (do shell script "echo " & quoted form of bItem & " | grep -Ei " & quoted form of myTerm)
            end try
        end repeat
    end repeat
    
    set text item delimiters to TID
    return matchList
    

    编辑 2

    set matchList to {}
    set firstList to {"jamesdoe", "jackknow", "henryrod"}
    set secondList to {"James Doe", "Jack Know", "John Matthews"}
    
    set {TID, text item delimiters} to {text item delimiters, {""}}
    repeat with aItem in firstList
        set AppleScript's text item delimiters to {""}
        set myTerm to text items of aItem
        set AppleScript's text item delimiters to {" ?"}
        set myTerm to myTerm as text
        repeat with bItem in secondList
            try
                do shell script "echo " & quoted form of bItem & " | grep -Ei " & quoted form of myTerm
                set end of matchList to (aItem's contents)
                exit repeat
            end try
        end repeat
    end repeat
    
    set text item delimiters to TID
    return matchList
    

    【讨论】:

    • 哇!我真的应该尝试更多地了解sed...这正是我想要的!虽然有点问题.. 我正在尝试与...合并... set list1 to {"jamesdoe","jackknow","henryrod"}set list2 to {"James Doe","Jack Know","John Matthews" 将返回 {"jamesdoe","jackknow"}..
    • 你能详细说明一下吗?
    • 抱歉,打enter 太快了。set list1 to {"jamesdoe","jackknow","henryrod"}set list2 to {"James Doe","Jack Know","John Matthews" 会返回{"jamesdoe","jackknow"}
    • 对不起,如果我把上面的评论写错了,我不知道如何得到{"jamesdoe", "jackknow"}而不是{"James Doe", "Jack Know"}
    猜你喜欢
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 2017-01-03
    • 2017-03-25
    • 1970-01-01
    相关资源
    最近更新 更多