元组/数组对的列表 [英] Tuples/ArrayList of pairs

查看:64
本文介绍了元组/数组对的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本质上是在尝试创建一个配对列表,事实证明这非常困难

I'm essentially trying to create a list of pairs which is proving frustratingly difficult

请注意,在任何人提到哈希表之前,都会有重复的内容,我不在乎.

Note before anyone mentions Hashtables that there will be duplicates which I don't care about.

例如,如果我这样做

$b = @{"dog" = "cat"}

我知道


Name                           Value
----                           -----
dog                            cat

这很好.但是,我无法添加喜欢的

which is good. However, I'm then unable to add the likes of

$b += @{"dog" = "horse"}

项目已添加.在字典中键入:'dog'被添加的密钥:'dog'

Item has already been added. Key in dictionary: 'dog' Key being added: 'dog'

我只是试图创建一个数据表,我可以使用.Add()+=将其添加到.

I'm just trying to create a table of data which I can add to using something like .Add() or +=.

推荐答案

Persistent13的有用答案提供了有效的解决方案-虽然有点晦涩.

Persistent13's helpful answer offers an effective and efficient solution - albeit a slightly obscure one.

创建哈希表的 array 是最简单的解决方案,但是要注意,扩展"数组意味着隐式地重新创建,因为数组是固定大小的数据结构,可能会因多次迭代而成为性能问题:

Creating an array of hashtables is the simplest solution, though it's important to note that "extending" an array means implicitly recreating it, given that arrays are fixed-size data structures, which can become a performance problem with many iterations:

# Using array-construction operator ",", create a single-element array
# containing a hashtable
$b = , @{ dog = "cat"} 

# "Extend" array $b by appending another hashtable.
# Technically, a new array must be allocated that contains the original array's
# elements plus the newly added one.
$b += @{ dog = "horse"}

此GitHub问题讨论了将来可能进行的增强,以使PowerShell本地有效地支持类似于列表的可扩展数据类型,或者甚至默认为此类数据类型. (从Windows PowerShell v5.1/PowerShell Core 6.2.0开始,PowerShell默认为类型为[object[]]的固定大小的数组.)

This GitHub issue discusses a potential future enhancement to make PowerShell natively support an efficiently extensible list-like data type or for it to even default to such a data type. (As of Windows PowerShell v5.1 / PowerShell Core 6.2.0, PowerShell defaults to fixed-size arrays, of type [object[]]).

这篇关于元组/数组对的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆