我有两个使用import-csv cmdlet导入的大型.csv文件。我做了很多搜索和尝试,最后发布帖子寻求帮助以简化此操作。
我需要遍历第一个数组,该数组的范围从80k行到500k行。这些数组中的每个对象都具有多个属性,然后我需要在第二个数组中找到相同大小的第二个数组中的对应条目,该数组与该属性上的某个属性匹配。
我将它们作为[systems.collection.arrayList]导入,我也尝试将它们作为哈希表放置。我什至尝试过在其他几篇文章中提到的LINQ。
任何人都可以提供建议或见解如何使运行速度更快?感觉就像我在一个干草堆中寻找在另一堆中匹配干草的情况。
$ImportTime1 = Measure-Command {
[System.Collections.ArrayList]$fileList1 = Import-csv file1.csv
[System.Collections.ArrayList]$fileSorted1 = ($fileList1 | Sort-Object -property 'Property1' -Unique -Descending)
Remove-Variable fileList1
}
$ImportTime2 = Measure-Command {
[System.Collections.ArrayList]$fileList2 = Import-csv file2.csv
[System.Collections.ArrayList]$fileSorted2 = ($fileList2 | Sort-Object -property 'Property1' -Unique -Descending)
Remove-Variable fileList2
}
$fileSorted1.foreach({
$varible1 = $_
$target = $fileSorted2.where({$_ -eq $variable1})
###do some other stuff
})
答案 0 :(得分:0)
这可能有用:https://powershell.org/forums/topic/comparing-two-multi-dimensional-arrays/
注释#27359中的更新解决方案+添加了Max Kozlov在注释#27380中建议的更改。
Function RJ-CombinedCompare() {
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$True)]$List1,
[Parameter(Mandatory=$True)]$L1Match,
[Parameter(Mandatory=$True)]$List2,
[Parameter(Mandatory=$True)]$L2Match
)
$hash = @{}
foreach ($data in $List1) {$hash[$data.$L1Match] += ,[pscustomobject]@{Owner=1;Value=$($data)}}
foreach ($data in $List2) {$hash[$data.$L2Match] += ,[pscustomobject]@{Owner=2;Value=$($data)}}
foreach ($kv in $hash.GetEnumerator()) {
$m1, $m2 = $kv.Value.where({$_.Owner -eq 1}, 'Split')
[PSCustomObject]@{
MatchValue = $kv.Key
L1Matches = $m1.Count
L2Matches = $m2.Count
L1MatchObject = $L1Match
L2MatchObject = $L2Match
List1 = $m1.Value
List2 = $m2.Value
}
}
}
$fileList1 = Import-csv file1.csv
$fileList2 = Import-csv file2.csv
$newList = RJ-CombinedCompare -List1 $fileList1 -L1Match $(yourcolumnhere) -List2 $fileList2 -L2Match $(yourothercolumnhere)
foreach ($item in $newList) {
# your logic here
}
将列表传递到此哈希表中应该很快,并且迭代遍历它也很快。