如何从Swift集合中获取多个随机元素?
我有一个set
,其中包含多个elements
。我的目标是从中获得10个随机元素
。
我如何以最简单的方式(希望是一行)实现此目标,而无需修改set set
本身或创建新的参考set
?
I have a Set
which contains multiple Elements
. My goal is to get 10 random Elements
out of it.
How can I achieve this goal in the simplest way (hopefully one line) without having to modify the Set
itself or create a new reference Set
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我想到的最短的单线:
它返回
arrayslice
,因此,如果您需要结果作为数组或另一组,则必须将其显式地施放为所需的类型。Here's the shortest one-liner I came up with:
It returns an
ArraySlice
though so if you need the result as an array or another set you'll have to explicitly cast it to your desired type.