gpt4 book ai didi

macos - Applescript,我试图让一个列表出现在一个对话框中,其中只有我拥有的总数的 3

转载 作者:行者123 更新时间:2023-12-02 03:19:30 25 4
gpt4 key购买 nike

Applescript,我试图让一个列表出现在对话框中,其中只有项目总数的 3 个。例如,我有一个包含 5 个可能项目的列表,我希望对话框显示 3 个随机项目,但不重复。我不知道我是否在正确的轨道上,但我卡住了。

set myList to {"Apples", "Bananas", "Oranges", "Grapes", "Turkey"}

set r to some item of myList

set newList to (myList without r)

set t to some item of newList

set newerList to (newList without t)

newList

最佳答案

与 vadian 不同的方法。 vadian 脚本的问题在于,理论上脚本可以永远运行,如果脚本不断地选择它之前拿过的项目。因此,最好有一个没有冲突的解决方案。这需要更多的努力,因为每次选择一个项目后,都必须从列表中删除该值。由于在 AppleScript 中没有执行此操作的简单命令,脚本必须“手动”执行此操作。

最简单的方法是创建一个包含输入列表索引的并行列表,在每次迭代中选择一个随机索引,并将其设置为非整数值。这样我们就可以确保该项目只被挑选一次。

set myList to {"Apples", "Bananas", "Oranges", "Grapes", "Turkey"}
set idxList to {}
-- first create a list with indexes
repeat with x from 1 to count myList
set end of idxList to x
end repeat

set newList to {}
repeat 3 times
-- pick a random index
set theIndex to some integer of idxList
-- add item to newlist based on picked index
set end of newList to item theIndex of myList
-- set the picked index to missing value so it will not be picked again
set item theIndex of idxList to missing value
end repeat
newList

关于macos - Applescript,我试图让一个列表出现在一个对话框中,其中只有我拥有的总数的 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34408541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com