【问题标题】:Adding Items to an array in a round robin fashion in powershell?在powershell中以循环方式将项目添加到数组中?
【发布时间】:2020-08-12 04:46:31
【问题描述】:

我正在启动一个项目,其中包含基本名称的 .csv 文件将作为输入。 我需要遍历此 .csv 中的所有名称,并将它们添加到最大限制为 20 的列表/数组中。此外,如果名称已经在列表中,那么它需要移动到下一个列表中。但是需要制作的列表/数组的数量是基于 csv 的总数。可以输入名称的次数没有限制,但我需要在所有数组中循环,以便名称均匀分布。

所以我需要类似 if ($CSVfilenames.count =

最好为此使用多维数组,或者任何人都可以更好地选择如何开始。我真的不知道如何开始,我尝试的前几件事导致了灾难。

在 power shell 方面还是很新的。

【问题讨论】:

  • 重复项是否可能多于列表?比如说,80 行 -> 4 个数组,但 5 行包含名称“Bob”?
  • 向我们展示一些灾难。也许他们只需要一点润色......
  • @vonPryz 是的,这是非常好的和预期的行动。有可能存在大量相同的名称,因此它只需要继续添加名称,但如果它添加多个条目,那么它甚至需要一个带有 2 和另一个带有 1 的 3 是好的,但是一个有 4 个,其他只有 1 个不是。

标签: arrays list powershell csv


【解决方案1】:

这是一个基于哈希表的快速草稿。首先,计算需要多少组。将输入的大小除以组大小并向上取整。然后创建用于存储元素的集合。遍历输入并将每个元素存储到一个集合中。

缺少的是如何处理重复项。查找是否存在很容易,因为哈希表有 .ContainsKey() 方法就是为了这个。如何获取另一个哈希表的逻辑留给读者作为练习。

此解决方案不保留原始顺序,因为哈希表根据定义是无序集。还有ordered dictionary,可以通过类型加速器[ordered]访问。

# Some test cases
$data = @('basil', 'marjoram', 'aniseed', 'parsely', 'chives', 'sage', 'fennel', 'oregano', 'thyme', 'tarragon', 'rosemary') # 11 unique elements
$data = @('basil', 'marjoram', 'aniseed', 'parsely', 'chives', 'sage', 'fennel', 'oregano', 'thyme', 'tarragon', 'rosemary', 'laurel') # 12 unique elements
$data = @('basil', 'marjoram', 'aniseed', 'parsely', 'parsely', 'chives', 'sage', 'fennel', 'oregano', 'thyme', 'tarragon', 'rosemary') # 12 elements, one duplicate, good index
$data = @('basil', 'marjoram', 'aniseed', 'parsely', 'chives', 'sage', 'fennel', 'parsely', 'oregano', 'thyme', 'tarragon', 'rosemary') # 12 elements, one duplicate, bad index

# How big a group will be
$groupMaxSize = 4
# Divide list size with group size and round up
# This many groups are needed
$numGroups = [math]::ceiling($data.Count / $groupMaxSize)

# Create hash table for each group. Store in an array
$hh = @()
for($i=0;$i -le $numGroups; ++$i) {
    $hh += @{}
}

# Iterate through the data. Each element index mod numGroups will tell
# into which group it will belong.
for($i=0;$i -lt $data.Count; ++$i) {
    # Which group will the element go into?
    $idx = $i % $numGroups
    # Hashtable requires unique keys, so only add elements that don't exist.
    # Key is the data, value is group index.
    if(-not $hh[$idx].ContainsKey($data[$i])) {
        $hh[$idx].Add($data[$i], $idx)
    } else {
        # Element alreayd existed, add logic to look up next free hashtable
        write-host "collision!" $data[$i]
    } 
}

# print results
$hh
collision!  parsely

Name                           Value
----                           -----
basil                          0
chives                         0
oregano                        0
thyme                          1
marjoram                       1
sage                           1
aniseed                        2
fennel                         2
tarragon                       2
rosemary                       3
parsely                        3

【讨论】:

  • 这太完美了!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-13
  • 2021-06-19
  • 1970-01-01
  • 2020-12-23
相关资源
最近更新 更多