String 在《朱莉娅》中,如何拒绝带有某种特征的词语?
我想收集不包含特定字符的单词 我觉得它应该比我目前的解决方案(严格使用正则表达式)简单得多 但是,唉,我就是不明白 这是我目前的解决方案,它有效:String 在《朱莉娅》中,如何拒绝带有某种特征的词语?,string,julia,list-comprehension,String,Julia,List Comprehension,我想收集不包含特定字符的单词 我觉得它应该比我目前的解决方案(严格使用正则表达式)简单得多 但是,唉,我就是不明白 这是我目前的解决方案,它有效: want(s) = match(r"\?",s) == nothing [s for s in lst if want(s)] 其他一切都会给我语法错误: [s for s in lst if not '?' in s] [s for s in lst if not ('?' in s)] filter((x) -> not ('?' in
want(s) = match(r"\?",s) == nothing
[s for s in lst if want(s)]
其他一切都会给我语法错误:
[s for s in lst if not '?' in s]
[s for s in lst if not ('?' in s)]
filter((x) -> not ('?' in x),["?asdas","bbb"])
我可以使用一个详细的三元运算符:
filter((x) -> ('?' in x ? false : true),["?asdas","bbb"])
但这似乎并不优雅
建议?
非
不是有效的关键字。julia中的否定操作是代码>。例如,此代码可以工作:
[s for s in lst if !('?' in s)]
下面是使用过滤器的更简洁的变体:
filter(x -> '?' ∉ x, ["?asdas","bbb"])
∉
基本上就是!在
中,可以写为\notin
。可能更有效的方法是使用正则表达式和match
as
filter(x -> isnothing(match(r"\?", x)), ["?asdas","bbb"])
这么简单。我知道有一个更优雅的解决方案。Thnx.
中的在这里很好,但是如果您使用occursin
可以过滤更一般的子字符串,甚至正则表达式。