转换为向量

发布于 2025-01-22 00:48:39 字数 292 浏览 4 评论 0原文

这似乎是一件非常简单的事情,但是相信与否我的Google搜索没有给我带任何地方。我希望能够将集合转换为向量。我已经尝试了以下内容:

sett=Set{Int64}([1,2,3])
vecc=convert(Vector{Int64},sett)

但是看来朱莉娅不知道该如何进行这些转换。

我能做的一件明显的事情是使用综合,例如

vecc=[el for el in sett]

,没有更优雅的方式吗?

提前欢呼。

This seems like a really easy thing, but believe it or not my google search hasn't brought me anywhere. I want to be able to convert a set to a vector. I have tried the following:

sett=Set{Int64}([1,2,3])
vecc=convert(Vector{Int64},sett)

but it appears Julia doesn't know how to do these conversions.

One obvious thing I can do is using comprehensions e.g.

vecc=[el for el in sett]

but is there not a more elegant way?

Cheers in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦魇绽荼蘼 2025-01-29 00:48:39

从性能的角度来看,分裂可能是有问题的。而是使用

vecc = collect(sett) 

Splatting can be problematic from a performance perspective. Instead, use

vecc = collect(sett) 
蓝色星空 2025-01-29 00:48:39

您可以使用 splat ocerator 操作员:

vecc = [sett...]

虽然,但是, (对我来说)比使用列表理解更优雅,即使有相对较小的收藏(由于每个元素至少需要一个分配!),也应该避免出现绩效问题的裂口。

因此,如果担心性能,您绝对应该使用collect()或坚持列表理解:

#itemscollect> collect(set)[i in set in set] in in set] < /code>[set ...]
10^164.934 NS(1 Alloc。)66.734 NS(1 Alloc。)856.716 NS NS(12 Alloc。)
10^32.511μs(1 Alloc。1 Alloc。)2.256μs(1个同种)83.900μs(2233 Alloc。)
10^69.627 ms(2个同种)10.181 ms(2 Alloc。)102.752 ms(2999281 Alloc。)

You can use the splat operator :

vecc = [sett...]

Though, while this is (to me) more elegant than using list comprehension, one should avoid splatting for performance concerns, even with relatively small collections (as it requires at least one allocation per element!).

So if concerned about performance, you should definitely use collect() or stick with list comprehension :

#itemscollect(set)[i for i in set][set...]
10^164.934 ns (1 alloc.)66.734 ns (1 alloc.)856.716 ns (12 alloc.)
10^32.511 μs (1 alloc.)2.256 μs (1 alloc.)83.900 μs (2233 alloc.)
10^69.627 ms (2 alloc.)10.181 ms (2 alloc.)102.752 ms (2999281 alloc.)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文