我有这个数组:
[
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]
并希望对其进行整理,并制作两个独立的数组,像这样
[
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo"
]
[
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]
希望能为您提供帮助!谢谢:)
答案 0 :(得分:1)
简单的数组过滤:
def highscore():
file_highscore = open('scores_test2.txt' , 'r')
scores_and_names = []
scores = []
scores_2 = []
names = []
for line in file_highscore.readlines():
score_info = line.split()
scores_and_names.append(line)
scores_2.append(scores_and_names[line][])
scores.append(score_info[1])
names.append(score_info[0])
scores.sort(key = int)
scores.sort(reverse = True)
print('The First 5 Highscores are:')
for item in scores[:5]:
print(item)
答案 1 :(得分:1)
另一种不同的方法是使用@BindingAdapter("imageUrl")
public static void setImageUrl(ImageView view) {
//Now get image view instance to provide width and height.
}
cmdlet对计算所得的属性进行分组。您没有指定要排序的确切部分-虚拟电子邮件或其后的名称-因此我在虚拟电子邮件之后使用了该部分。
结果为您提供了一个数组的集合,这些数组的名称为[计算出的属性],一组为[与该计算出的属性匹配的项目]。您可以单独处理它们,也可以使用它们来制作新阵列。
Group-Object
输出...
$AllNames = @(
'org-dummyemail-123-AccountName',
'org-dummyemail-123-Email',
'org-dummyemail-123-PAT',
'org-dummyemail-123-PATName',
'org-dummyemail-123-PATValidTo'
'org-dummyemail1-joemarmoto-AccountName',
'org-dummyemail1-joemarmoto-Email',
'org-dummyemail1-joemarmoto-PAT',
'org-dummyemail1-joemarmoto-PATName',
'org-dummyemail1-joemarmoto-PATValidTo'
)
# 3rd item index = 2
$PartToGroupOn = 2
$GroupedAllNames = $AllNames |
Group-Object -Property {$_.Split('-')[$PartToGroupOn]}
$GroupedAllNames[1].Group
答案 2 :(得分:0)
将输入作为Json对象处理(来自here字符串) 脚本
-
/破折号分隔的部分中构建密钥
计算的属性。 $Json = @"
[
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]
"@ | ConvertFrom-JSon
$Json | Select-Object @{n='key';E={($_ -split '-')[0..2] -join '-'}},
@{n='value';e={$_}} |
Group-Object key | ForEach-Object{
$_.Group.Value | ConvertTo-Json
}
具有以下输出:
[
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo"
]
[
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]
答案 3 :(得分:0)
使用$ PSItem变量
$arr="org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
$arr2 = $arr | Where-Object {$PSItem -match 'joemarmoto'}
$arr3 = $arr | Where-Object {$PSItem -match '123'}
从该站点: 获取有关about_Automatic_Variables -ShowWindow的帮助
在“查找”框中键入“ psitem”以突出显示包含该单词的所有文本:
我们可以看到,$ PSItem与$ _相同。引入$ PSItem变量的目的是使包含“管道中当前对象”的代码更易于阅读和理解。
答案与Gert Jan Kraaijeveld几乎完全相同,但是很清楚$ PSItem的作用以及该信息的来源。
答案 4 :(得分:0)
以下是使用正则表达式确定输入字符串的哪一部分分组依据的版本:
clear-host
'REGEX 1; org-dummyemail-123 vs org-dummyemail1-joemarmoto'
$groupedByAllButLast = @(
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
) | Group-Object -Property @{E={$_ -replace '([^-]+-[^-]+-[^-]+)-.*', '$1'}}
for ([int]$i = 0; $i -lt $groupedByAllButLast.Count; $i++)
{
Write-Verbose $groupedByAllButLast[$i].Name -Verbose
$groupedByAllButLast[$i].Group
}
'REGEX 2; 123 vs joemarmoto'
$groupedByTheNameOrEmailBit = @(
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
) | Group-Object -Property @{E={$_ -replace '[^-]+-[^-]+-([^-]+)-.*', '$1'}} #all we've changed is the location of the opening bracket in this line
for ([int]$i = 0; $i -lt $groupedByTheNameOrEmailBit.Count; $i++)
{
Write-Verbose $groupedByTheNameOrEmailBit[$i].Name -Verbose
$groupedByTheNameOrEmailBit[$i].Group
}
NB:这基本上与Lee's相同;仅使用regex捕获字符串中与我们的键相关的所有部分,而不是使用split
并取一个符号段...有关此有用功能的更多信息,请参见MS Docs
答案 5 :(得分:0)
相同的想法,但带有哈希表:
API