And*_*ew -1 python data-structures
请原谅标题中含糊不清的内容 - 我不太清楚如何表达我的问题.
给定一个字符串:
blah = "There are three cats in the hat"
Run Code Online (Sandbox Code Playgroud)
和(我不太确定使用哪个数据结构)"userInfo":
cats -> ("tim", "1 infinite loop")
three -> ("sally", "123 fake st")
three -> ("tim", "1 infinite loop")
three cats -> ("john", "123 fake st")
four cats -> ("albert", "345 real road")
dogs -> ("tim", "1 infinite loop")
cats hat -> ("janet", NULL)
Run Code Online (Sandbox Code Playgroud)
适当的输出应该是:
tim (since 'cats' exists)
sally (since 'three' exists)
tim (since 'three' exists)
john (since both 'three' and 'cats' exist)
janet (since both 'cats' and 'hat' exist somewhere in the string blah)
Run Code Online (Sandbox Code Playgroud)
我想要一种有效的方式来存储这些数据.有可能匹配多个"三个"字符串(即150个人将拥有该字符串.)我是否应该拥有包含所有这些数据的列表并复制"密钥"?
像这样的东西?
class Content( object ):
def __init__( self, content, maps_to ):
self.content= content.split()
self.maps_to = maps_to
def matches( self, words ):
return all( c in words for c in self.content )
def __str__( self ):
return "%s -> %r" % ( " ".join(self.content), self.maps_to )
rules = [
Content('cats',("tim", "1 infinite loop")),
Content('three',("sally", "123 fake st")),
Content('three',("tim", "1 infinite loop")),
Content('three cats',("john", "123 fake st")),
Content('four cats',("albert", "345 real road")),
Content('dogs',("tim", "1 infinite loop")),
Content('cats hat', ("janet", None)),
]
blah = "There are three cats in the hat"
for r in rules:
if r.matches(blah.split()):
print r
Run Code Online (Sandbox Code Playgroud)
产量
cats -> ('tim', '1 infinite loop')
three -> ('sally', '123 fake st')
three -> ('tim', '1 infinite loop')
three cats -> ('john', '123 fake st')
cats hat -> ('janet', None)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1304 次 |
最近记录: |