[英]Group two arrays of objects in powershell
我有兩個對象數組。 如果找到,我想在“car”數組中插入相應的 extraProperty。 可能有幾個 extraProperties 或沒有。 也就是說,當為相應的汽車找到一個額外屬性時,我想添加一個包含找到的額外屬性列表的數組。
每個 extraProperties 由一個 object 組成,具有以下屬性:Id、Name、Value。
代碼:
param(
[Parameter(Mandatory=$false, Position=1, ValueFromPipeline=$false)]
[string]$Types,
[Parameter(Mandatory=$false, Position=2, ValueFromPipeline=$false)]
[string]$PathFile,
[Parameter(Mandatory=$false, Position=3, ValueFromPipeline=$false)]
[string]$PathPropertyFile
)
$profiles_list = Import-Csv $PathFile -Header Id, model, Type Delimiter ";"
$extraProperties_list = Import-Csv $PathPropertyFile -Header ProfileId,Name,Value -Delimiter ";" # Get-Content -Path $pathFile
foreach($p in $car_list) {
$Property = $property_list.Where({$_.Id -eq $p.Id}) | Select-Object -Property Name,Value
if(-Not (($null -eq $Property ) -And (@($Property ).Count -eq 0)) ) {
$p = $p | Add-Member -NotePropertyMembers @{Properties=$Property }
} else {
$p = $p | Add-Member -NotePropertyMembers @{Properties=@()}
}
}
數據樣本:
PropertyFile.csv
Id | Name | Value
504953 | Example1 | Value1
504953 | Example2 | Value2
504955 | Example3 | Value3
CarFiles.csv
Id | Model | Type
504953 | Model1 | 3
504954 | Model1 | 0
504955 | Model3 | 3
問題是代碼效率不高。 汽車陣列達到 200000 個位置,其中每個 position 是具有多個屬性的 object,並且屬性陣列也達到這些值。 該腳本需要無數小時才能執行。
有什么方法可以優化在 arrays 中插入新屬性?
您需要檢查這是否更快(我們不知道 CSV 文件有多大),但您可以這樣做:
對於演示,我使用 Here-Strings,但在現實生活中,您從文件中導入數據:
$profiles_list = Import-Csv $PathFile -Delimiter ";"
$extraProperties_list = Import-Csv $PathPropertyFile -Delimiter ";"
使用您的示例:
$profiles_list = @"
Id;Model;Type
504953;Model1;3
504954;Model1;0
504955;Model3;3
"@ | ConvertFrom-Csv -Delimiter ';'
$extraProperties_list = @"
Id;Name;Value
504953;Example1;Value1
504953;Example2;Value2
504955;Example3;Value3
"@ | ConvertFrom-Csv -Delimiter ';'
# get the headers from the Cars
$profileHeaders = $profiles_list[0].PsObject.Properties.Name
# get the new property names from the ExtraProperties Name column
$newHeaders = $extraProperties_list.Name | Where-Object {$profileHeaders -notcontains $_} | Select-Object -Unique
# add all these new properties to the $profiles_list, for now with $null values
$profiles_list | ForEach-Object {
foreach($prop in $newHeaders) {
$_ | Add-Member -MemberType NoteProperty -Name $prop -Value $null
}
}
# group the $extraProperties_list by the Id column and loop through these groups
$extraProperties_list | Group-Object Id | ForEach-Object {
$id = $_.Name
# get an array of profiles with matching Ids
$profiles = $profiles_list | Where-Object {$_.Id -eq $id}
# and fill in the blanks
foreach($item in $profiles) {
foreach($extra in $_.Group) {
$item.($extra.Name) = $extra.Value
}
}
}
# output on screen
$profiles_list
# output to new CSV file
$profiles_list | Export-Csv -Path 'X:\CompletedProfiles.csv' -Delimiter ';' -NoTypeInformation
屏幕上的結果:
Id : 504953 Model : Model1 Type : 3 Example1 : Value1 Example2 : Value2 Example3 : Id : 504954 Model : Model1 Type : 0 Example1 : Example2 : Example3 : Id : 504955 Model : Model3 Type : 3 Example1 : Example2 : Example3 : Value3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.