有没有办法让difflib考虑删除字符串匹配?
我已经尝试了difflib.get_close_matches()但是它不考虑在关闭匹配输出中具有较低长度的字符串.例如
from difflib import get_close_matches as gcm
x = """Erfreulich
Erfreuliche
Erfreulicher
Erfreulicherem
Erfreulicheres
Erfreulicherweis
Erfreulicherweise
Erfreuliches
Erfreulichste"""
x = [i for i in x.split("\n")]
for i in x:
print i, gcm(i,x)
Run Code Online (Sandbox Code Playgroud)
输出:
Erfreulich ['Erfreulich', 'Erfreuliche', 'Erfreuliches']
Erfreuliche ['Erfreuliche', 'Erfreuliches', 'Erfreulicher']
Erfreulicher ['Erfreulicher', 'Erfreuliche', 'Erfreulicheres']
Erfreulicherem ['Erfreulicherem', 'Erfreulicheres', 'Erfreulicher']
Erfreulicheres ['Erfreulicheres', 'Erfreulicherweis', 'Erfreulicherem']
Erfreulicherweis ['Erfreulicherweis', 'Erfreulicherweise', 'Erfreulicheres']
Erfreulicherweise ['Erfreulicherweise', 'Erfreulicherweis', 'Erfreulicheres']
Erfreuliches ['Erfreuliches', 'Erfreuliche', 'Erfreulicheres']
Erfreulichste ['Erfreulichste', 'Erfreuliche', 'Erfreuliches']
Run Code Online (Sandbox Code Playgroud)
请注意,对于字符串Erfreulicher,Erfreulich虽然距离仅为-1,但不会被视为近似匹配.
是否有可能通过模式匹配来检测不属于案例类的类的子类型?
我需要使用现有的Java类,所以我不能声明我自己的case类.
我在log4j xml文件中有以下布局模式:
"%d{ISO8601} %c %p [%t] [%x] 9.5.4.RC12 %m%n"
我想要的是,当我得到包含消息的日志时process [proc#] completed,应该跳过它.我的意思是应该打印除包含此消息的日志之外的所有日志.[proc#]将包含最大长度的进程号4.我可以在我的xml配置文件中使用此函数设置过滤器.如果是这样,那怎么样?
来自LearnYouAHaskell:
还有一件事 - 你不能在模式匹配中使用++.如果你试图模式匹配(xs ++ ys),那么第一个和第二个列表中的内容是什么?这没有多大意义.匹配(xs ++ [x,y,z])或只是(xs ++ [x])的东西是有意义的,但由于列表的性质,你不能这样做.
我正在努力通过列表的性质来解释他的意思,为什么这不可能.
我想在PostgreSQL SELECT查询中的字符'/'后提取字符串.
字段名称是source_path,表名是movies_history.
数据示例:
source_path的值:
等等.source_path的所有值都采用此格式
我只需要获取'file.xxx'字符串.
我有一个模式匹配其参数的函数,它是string:
let processLexime lexime
match lexime with
| "abc" -> ...
| "bar" -> ...
| "cat" -> ...
| _ -> ...
Run Code Online (Sandbox Code Playgroud)
这按预期工作.但是,我现在试图通过表达"匹配string仅包含以下字符" 来扩展它.在我的具体示例中,我希望只匹配包含数字的任何内容.
我的问题是,我怎样才能在F#中表达这一点?我更喜欢在没有任何库的情况下这样做FParsec,因为我主要是为了学习目的而这样做.
假设我有一个函数的两个用例.对具有订阅的用户和具有订阅的用户执行某些操作.
def add_subscription(subscription_id, %User{subscription: nil})
# Do something with user here.
Run Code Online (Sandbox Code Playgroud)
和
def add_subscription(subscription_id, user)
Run Code Online (Sandbox Code Playgroud)
我如何进行这样的模式匹配,还是在第一个函数中使用用户?
谢谢!
按照观察的最小例子(那种惊讶我):
type Vector = V of float*float
// complete unfolding of type is OK
let projX (V (a,_)) = a
// also works
let projX' x =
match x with
| V (a, _) -> a
// BUT:
// partial unfolding is not Ok
let projX'' (V x) = fst x
// consequently also doesn't work
let projX''' x =
match x with
| V y -> fst y
Run Code Online (Sandbox Code Playgroud)
是什么原因导致无法与部分解构的类型相匹配?
一些部分解构似乎没问题:
// Works
let f (x,y) = fst y
Run Code Online (Sandbox Code Playgroud)
编辑:好的,我现在理解所描述行为的"技术"原因(感谢您的回答和评论).但是,我认为语言明智,与其他语言相比,这种行为感觉有点"不自然": …
在Elm中有一种方法可以将函数的参数与Haskell中的多个定义进行模式匹配吗?
Haskell的例子:
factorial :: Int ->
factorial 0 = 1
factorial n = n * factorial (n - 1)
Run Code Online (Sandbox Code Playgroud) 我在redis中有以下键:
"542 136 mountain road"
"542 136 mountainview road"
"542136 mountain road"
"542 136 mountain"
"136 mountain road"
"136 mountain"
Run Code Online (Sandbox Code Playgroud)
我想得到包含这个短语的键136 mountain.使用glob样式模式,我目前正在进行4次查询,以满足所有可能的情况.
scan 0 MATCH '*[\ ]136 mountain[\ ]*'
scan 0 MATCH '*[\ ]136 mountain'
scan 0 MATCH '136 mountain[\ ]*'
scan 0 MATCH '136 mountain'
Run Code Online (Sandbox Code Playgroud)
总共这四个查询将返回4个结果:
"542 136 mountain road"
"542 136 mountain"
"136 mountain road"
"136 mountain"
Run Code Online (Sandbox Code Playgroud)
如果有更好的方法来更改模式字符串,请分享您的输入,以便可以在单个查询中获得所有4个结果.
pattern-matching ×10
f# ×2
case-class ×1
constructor ×1
difflib ×1
elixir ×1
elm ×1
glob ×1
haskell ×1
list ×1
log4j ×1
postgresql ×1
python ×1
redis ×1
regex ×1
scala ×1
sql ×1
string ×1
types ×1