【问题标题】:Python: Sliding Window to compare words in my list with words in the given lexiconPython:滑动窗口将列表中的单词与给定词典中的单词进行比较
【发布时间】:2014-09-01 04:17:10
【问题描述】:

我想使用幻灯片窗口来检查“文档”中的单词是否与“词典”中的单词匹配。 我的问题是:哪个主要功能在做这种事情?我看了几个滑动窗口的例子,似乎不是我想要的。

document=['hello','my','world','love'], lexicon=['questions','hello','shift',.....]

如果我使用大小设置为 3 的幻灯片窗口。这是否意味着我会得到 ​​p>

('hello','my','world') and ('my','world','love')

对于每一个,我想测试一下

'hello','my','world' 

分别在词典中,然后测试是否

'hello my','my world', 'hello world' 

分别在词典中,并测试是否

'hello my world'

在词典里。

【问题讨论】:

  • 你有什么问题?
  • @PauloBu 我的问题是哪个主函数在做这种事情?

标签: python sliding-window


【解决方案1】:
import itertools 

document=['hello','my','world','love']

for x in range(len(document)-2):
    words = document[x:x+3]
    print 'words:', words

    print "--- 1 ---"
    for y in itertools.combinations(words,1):
        print ' '.join(y)

    print "--- 2 ---"
    for y in itertools.combinations(words,2):
        print ' '.join(y)

    print "--- 3 ---"
    print ' '.join(words)

    print "-----------------"

结果

words: ['hello', 'my', 'world']
--- 1 ---
hello
my
world
--- 2 ---
hello my
hello world
my world
--- 3 ---
hello my world
-----------------
words: ['my', 'world', 'love']
--- 1 ---
my
world
love
--- 2 ---
my world
my love
world love
--- 3 ---
my world love
-----------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-07
    • 2012-11-07
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多