有没有一个“集合”? bash 中的数据结构?

发布于 2024-11-30 15:10:21 字数 87 浏览 0 评论 0原文

Python 有一个“集合”类型,其中包含唯一的对象。 Bash 有类似的东西吗?

我想继续向这样的 bash“集”添加元素,并且永远不要重复。

Python has a "set" type which contains unique objects.
Does Bash have something equivalent?

I want to keep adding elements to such a bash "set" and never have duplicates.

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

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

发布评论

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

评论(3

过期以后 2024-12-07 15:10:21

Bash 4.0 具有关联数组,可用于构建集合。

这里有一篇文章讨论了它们(这是第一篇 Google 搜索的“bash 关联数组”)。

(就我个人而言,我只会使用 bash 以外的东西,可能是 Perl。)

Bash 4.0 has associative arrays, which can be used to build sets.

Here's an article that discusses them (it was the first Google hit for "bash associative arrays").

(Personally, I'd just use something other than bash, probably Perl.)

赢得她心 2024-12-07 15:10:21

经过一番谷歌搜索后,我在 http:// www.catonmat.net/blog/set-operations-in-unix-shell-simplified/。它以多种方式提供所有常用的集合运算符,甚至还有可打印的 pdf 备忘单。

不过,我使用了关联数组,它更具可读性。

声明一个 setA 关联数组变量:

$ declare -A setA

或者同时声明并添加初始成员:

$ declare -A setA=([memberA]=1 [memberB]=1)

将成员添加到集合中:

$ setA[memberC]=1

测试成员身份:

$ [ -n "${setA[memberC]}" ] && echo in set || echo not in set
in set

$ [ -n "${setA[memberD]}" ] && echo in set || echo not in set
not in set

列出成员(空格分隔):

$ echo "${!setA[@]}"
memberA memberC memberB

或(换行符分隔):

$ printf '%s\n' "${!setA[@]}"
memberB
memberC
memberA

迭代成员:

$ for m in "${!setA[@]}"; do echo "$m"; done
memberB
memberC
memberA

基数(集合中的成员数量):

$ echo ${#setA[@]}
3

删除成员:

$ unset setA[memberC]

使用引号添加名称中带有空格的成员:

$ setA["member with space"]=1

也可以使用变量作为成员:

$ read -r str
$ setA["$str"]=1

After some googling I found a nice bash implementation at http://www.catonmat.net/blog/set-operations-in-unix-shell-simplified/. It has all the usual set operators in multiple ways, and even a printable pdf cheatsheet.

I've used associative arrays though, it's much more readable.

Declare a setA associative array variable:

$ declare -A setA

Or declare and add the initial members at the same time:

$ declare -A setA=([memberA]=1 [memberB]=1)

Add a member to the set:

$ setA[memberC]=1

Test for membership:

$ [ -n "${setA[memberC]}" ] && echo in set || echo not in set
in set

$ [ -n "${setA[memberD]}" ] && echo in set || echo not in set
not in set

List members (space separated):

$ echo "${!setA[@]}"
memberA memberC memberB

or (newline separated):

$ printf '%s\n' "${!setA[@]}"
memberB
memberC
memberA

Iterate through members:

$ for m in "${!setA[@]}"; do echo "$m"; done
memberB
memberC
memberA

Cardinality (number of members in the set):

$ echo ${#setA[@]}
3

Remove a member:

$ unset setA[memberC]

Use quotes to add members with spaces in their name:

$ setA["member with space"]=1

And it's possible to use variables as members too:

$ read -r str
$ setA["$str"]=1
哆啦不做梦 2024-12-07 15:10:21
some_dups=(aa aa b b c)
uniques=($(for v in "${some_dups[@]}"; do echo "$v";done| sort| uniq| xargs))
echo "${uniques[@]}"

在 bash 3 中也给出

aa b c

,其中没有可用的关联数组

some_dups=(aa aa b b c)
uniques=($(for v in "${some_dups[@]}"; do echo "$v";done| sort| uniq| xargs))
echo "${uniques[@]}"

gives

aa b c

also in bash 3, where no associative arrays are available

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文