0

我有 2 个列表,我需要将第一个列表中包含的元素添加到第二个列表中。如果第二个列表包含第一个列表的某些元素,则不应将这些元素添加到第二个列表中(以避免重复)。

目前我正在使用:

set ListB to ListB & ListA

但显然这些没有考虑到重复。

4

2 回答 2

2

当列表仅包含字符串且不包含换行符时,如 adayzdone 示例代码中所示,当两个列表都包含数千个项目时,您可以使用更快的方法。

--NOTE: Only works with lists containing strings and not containing linefeeds. 
set listA to {"A1", "A2", "A3"}
set listB to {"B1", "B2", "A3"}

set AppleScript's text item delimiters to linefeed
set newList to every paragraph of (do shell script "sort -fu <<< " & quoted form of ((listA as string) & linefeed & listB as string))
set AppleScript's text item delimiters to ""

return newList
于 2012-11-12T15:52:53.953 回答
1

尝试:

set listA to {"A1", "A2", "A3"}
set listB to {"B1", "B2", "A3"}

repeat with anItem in listB
    if anItem is not in listA then
        set end of listA to contents of anItem
    end if
end repeat

return listA
于 2012-11-11T15:25:27.000 回答