gpt4 book ai didi

regex - DataArray 不区分大小写的匹配,返回匹配的索引值

转载 作者:行者123 更新时间:2023-12-01 22:38:39 25 4
gpt4 key购买 nike

我的函数内部有一个 DataFrame:

using DataFrames

myservs = DataFrame(serverName = ["elmo", "bigBird", "Oscar", "gRover", "BERT"],
ipAddress = ["12.345.6.7", "12.345.6.8", "12.345.6.9", "12.345.6.10", "12.345.6.11"])
myservs
5x2 DataFrame
| Row | serverName | ipAddress |
|-----|------------|---------------|
| 1 | "elmo" | "12.345.6.7" |
| 2 | "bigBird" | "12.345.6.8" |
| 3 | "Oscar" | "12.345.6.9" |
| 4 | "gRover" | "12.345.6.10" |
| 5 | "BERT" | "12.345.6.11" |

如何编写函数来获取名为 server 的单个参数,不区分大小写,与 myservs[:serverName] 中的 server 参数匹配 code> DataArray,并返回匹配对应的ipAddress?

在 R 中,这可以通过使用来完成

myservs$ipAddress[grep("server", myservs$serverName, ignore.case = T)]

我不希望有人使用 ElMoElmo 作为 服务器,或者 serverName 保存为 elmoELMO

最佳答案

我引用了如何在 R 中完成任务并尝试使用 DataFrames pkg 来完成此任务,但我这样做只是因为我来自 R 并且我刚刚学习Julia。我向同事询问了很多问题,以下是我们得出的结论:

This task is much cleaner if I was to stop thinking in terms of vectors in R. Julia runs plenty fast iterating through a loop.

Even still, looping wouldn't be the best solution here. I was told to look into Dicts (check here for an example). Dict(), zip(), haskey(), and get() blew my mind. These have many applications.

My solution doesn't even need to use the DataFrames pkg, but instead uses Julia's Matrix and Array data representations. By using let we keep the global environment clutter free and the server name/ip list stays hidden from view to those who are only running the function.

In the sample code, I'm recreating the server matrix every time, but in reality/practice I'll have a permission restricted delimited file that gets read every time. This is OK for now since the delimited files are small, but this may not be efficient or the best way to do it.

# ONLY ALLOW THE FUNCTION TO BE SEEN IN THE GLOBAL ENVIRONMENT
let global myIP

# SERVER MATRIX
myservers = ["elmo" "12.345.6.7"; "bigBird" "12.345.6.8";
"Oscar" "12.345.6.9"; "gRover" "12.345.6.10";
"BERT" "12.345.6.11"]

# SERVER DICT
servDict = Dict(zip(pmap(lowercase, myservers[:, 1]), myservers[:, 2]))

# GET SERVER IP FUNCTION: INPUT = SERVER NAME; OUTPUT = IP ADDRESS
function myIP(servername)
sn = lowercase(servername)
get(servDict, sn, "That name isn't in the server list.")
end
end

​# Test it out
myIP("SLIMEY")
​#>​"That name isn't in the server list."

myIP("elMo"​)
#>​"12.345.6.7"

关于regex - DataArray 不区分大小写的匹配,返回匹配的索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29426542/

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