英文:
How to match two string arrays - 3 conditions?
问题
我这里有一个逻辑问题。我需要按照以下格式打印字符串:"user@email.com group@email.com",对于每个用户,其业务职称和经理与组CSV中的业务职称、部门和经理相匹配。如果有多个匹配的组,字符串应该按照上述格式打印,但是加上另一个组。在这里,最好的方法是什么?从两个CSV文件中创建数组,然后在循环中进行if判断吗?
示例
GroupCSV:
Group,Job title,Department,Manager (email address)
somesalesgroup@dundermifflin.com,Senior Sales Manager,Sales,michael.scott@dundermifflin.com
anothersalesgroup@dundermifflin.com,Senior Sales Manager,Sales,michael.scott@dundermifflin.com
UserCSV:
First name,Last name,Location,Start date,Job title,Department,Manager (email address)
Jim,Halpert,Scranton,7/1/2021,Senior Sales Manager,Sales,michael.scott@dundermifflin.com
Dwight,Schrute,Scranton,7/1/2021,Assistant to the Regional Manager,Sales,michael.scott@dundermifflin.com
我希望输出如下:
[jim.halpert@dundermifflin@takeaway.com somesalesgroup@dundermifflin.com]
[jim.halpert@dundermifflin@takeaway.com anothersalesgroup@dundermifflin.com]
目前我有以下代码
var match [][]string
for _, u := range userRows {
for _, g := range groupRows {
if u[0] == g[0] {
match = append(match, string{g, u})
}
}
}
但我不确定这里可能出了什么问题(string{g, u)。
英文:
I have a logical question here. I need to print a string in the format "user@email.com group@email.com" for each user whose business title and manager fit the business title, department and manager in the group CSV. If there's more than one matching group the string should be printed as above but with another group. What would be the best approach here? Create arrays from both CSV files then do an if in the loop?
Example
GroupCSV:
Group,Job title,Department,Manager (email address)
somesalesgroup@dundermifflin.com,Senior Sales Manager,Sales,michael.scott@dundermifflin.com
anothersalesgroup@dundermifflin.com,Senior Sales Manager,Sales,michael.scott@dundermifflin.com
UserCSV:
First name,Last name,Location,Start date,Job title,Department,Manager (email address)
Jim,Halpert,Scranton,7/1/2021,Senior Sales Manager,Sales,michael.scott@dundermifflin.com
Dwight,Schrute,Scranton,7/1/2021,Assistant to the Regional Manager,Sales,michael.scott@dundermifflin.com
I would like to have an output like:
[jim.halpert@dundermifflin@takeaway.com somesalesgroup@dundermifflin.com]
[jim.halpert@dundermifflin@takeaway.com anothersalesgroup@dundermifflin.com]
At the moment I've got this
var match [][]string
for _, u := range userRows {
for _, g := range groupRows {
if u[0] == g[0] {
match = append(match, string{g, u})
}
}
}
But I'm not sure what may be wrong here (string{g, u})
答案1
得分: 0
解决方法如下:
var match [][]string
for _, u := range userRows {
for _, g := range groupRows {
if u[6] == g[1] && u[2] == g[0] {
match = append(match, []string{u[5], g[2]})
}
}
}
这段代码的作用是遍历userRows
和groupRows
两个二维字符串数组,找出满足条件u[6] == g[1] && u[2] == g[0]
的元素,并将其第5个和第3个元素组成一个新的字符串数组,然后将该数组添加到match
中。
英文:
Resolved this way:
var match [][]string
for _, u := range userRows {
for _, g := range groupRows {
if u[6] == g[1] && u[2] == g[0] {
match = append(match, []string{u[5], g[2]})
}
}
}
答案2
得分: -2
我可以想到的方法是将GroupCSV转换为一个映射(map),然后读取UserCSV来匹配这个映射。
如果读取CSV文件的过程是一个“for”循环,总共会有两个“for”循环。
英文:
The way I can think of is to convert GroupCSV into a map, and then read UserCSV to match the map.
If the process of reading the CSV file is a "for", there are two "for" in total.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论