提问人:Sudoh 提问时间:7/24/2022 最后编辑:Sudoh 更新时间:7/25/2022 访问量:146
如何在 Julia 中对多个值使用“endswith()”?
How to use `endswith()` against multiple values in Julia?
问:
我有一个,我想在它上面使用,但针对多个值。string
endswith()
我的第一个猜测是用元组来尝试:
# {string}
suffixes = ({multiple suffixes here})
endswith(i,extensions)
这生成了错误消息:
MethodError: no method matching endswith(::String, ::Tuple{String, String})
所以,我去 Julia 文档中寻找官方文档,但他们只谈论单字符串比较。
我确实在一个非官方网站上找到了这个,上面写着:
如果第二个参数是向量或字符集,则测试的最后一个字符是否属于该集。
string
这并不完全适用。
我尝试了以下变体:
endswith(i,extensions[:])
产生与以前相同的错误MethodError: no method matching endswith(::String, ::Tuple{String, String})
列表的变体:
# {string}
suffixes = [{multiple suffixes here}]
endswith(i,extensions)
仅将错误消息从元组更改为向量
MethodError: no method matching endswith(::String, ::Vector{String})
或提供索引
# {string}
suffixes = [{multiple suffixes here}]
endswith(i,extensions[:])
同样的错误
MethodError: no method matching endswith(::String, ::Vector{String})
我尝试了元组和向量,但也没有用。endswith(i,extensions[1:length(extensions)])
有人熟悉吗?
答:
您可能需要以下内容:
julia> endswith.("abcd", ["d", "c", "cd", "dc"])
4-element BitVector:
1
0
1
0
请注意 .此操作通过集合(在本例中为后缀向量)广播函数。有关详细信息,请参阅此处。.
endswith
评论
non-boolean (BitVector) used in boolean context
push!(my_list,i)
TypeError: non-boolean (BitVector) used in boolean context
endswith(i,r".item_one|item_two|...")
似乎以下情况也是可能的:
endswith("{string}",r"item_one|item-two|item_three|item_four") # items being what I want to check the string for.
如果我的项目是动态的,我想我可以使用 f 字符串来创建 r“{item_list_here}”
评论
Regex(join(suffixes, "|"))
评论
string
string
suffixes
file.pep
[".pep",".txt",".xlsx']
.pep
file.pep