gpt4 book ai didi

arrays - PowerShell:两个以上数组的交集

转载 作者:行者123 更新时间:2023-12-02 15:50:33 43 4
gpt4 key购买 nike

使用 PowerShell,我有 14 个字符串数组。一些数组是空的。我将如何获得这些数组(不包括空数组)的交集(所有数组中存在的所有元素)?我试图避免一次比较两个数组。

一些数组是空的,所以我不想在我的比较中包括那些。关于我将如何处理这个问题的任何想法?谢谢。

$a = @('hjiejnfnfsd','test','huiwe','test2')
$b = @('test','jnfijweofnew','test2')
$c = @('njwifqbfiwej','test','jnfijweofnew','test2')
$d = @('bhfeukefwgu','test','dasdwdv','test2','hfuweihfei')
$e = @('test','ddwadfedgnh','test2')
$f = @('test','test2')
$g = @('test','bjiewbnefw','test2')
$h = @('uie287278hfjf','test','huiwhiwe','test2')
$i = @()
$j = @()
$k = @('jireohngi','test','gu7y8732hbj','test2')
$l = @()
$m = @('test','test2')
$n = @('test','test2')

我尝试解决这个问题(虽然它不检查空数组):

$overlap = Compare-Object $a $b -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $c -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $d -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $e -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $f -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $g -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $h -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $i -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $j -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $k -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $l -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $m -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $n -PassThru -IncludeEqual -ExcludeDifferent

我想要的结果是 test 和 test2 出现在 $overlap 中。此解决方案不起作用,因为它不检查正在比较的数组是否为空。

最佳答案

注意:以下假设没有单个数组多次包含相同的字符串(需要做更多的工作来解决)。

$a = @('hjiejnfnfsd','test','huiwe','test2')
$b = @('test','jnfijweofnew','test2')
$c = @('njwifqbfiwej','test','jnfijweofnew','test2')
$d = @('bhfeukefwgu','test','dasdwdv','test2','hfuweihfei')
$e = @('test','ddwadfedgnh','test2')
$f = @('test','test2')
$g = @('test','bjiewbnefw','test2')
$h = @('uie287278hfjf','test','huiwhiwe','test2')
$i = @()
$j = @()
$k = @('jireohngi','test','gu7y8732hbj','test2')
$l = @()
$m = @('test','test2')
$n = @('test','test2')

$allArrays = $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n

# Initialize a hashtable in which we'll keep
# track of unique strings and how often they occur.
$ht = @{}

# Loop over all arrays.
$nonEmptyArrayCount = 0
foreach ($arr in $allArrays) {
# Loop over each non-empty array's elements.
if ($arr.Count -gt 0) {
++$nonEmptyArrayCount
foreach ($el in $arr) {
# Add each string and increment its occurrence count.
$ht[$el] += 1
}
}
}

# Output all strings that occurred in every non-empty array
$ht.GetEnumerator() |
Where-Object Value -eq $nonEmptyArrayCount |
ForEach-Object Key

以上输出那些存在于所有非空输入数组中的字符串:

test2
test

关于arrays - PowerShell:两个以上数组的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72608938/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com