在另一个字符串

时间:2015-08-09 14:27:53

标签: python

我取一组字符并生成它们的排列,产生一组字符串。然后,我检查特定文本文件中的任何字符串是否存在于结果排列中。也就是说,如果其中一个排列产生了像gtryop这样的词,那就试试'在文本文件中,我将能够知道并打印它。但是,我的代码似乎无法正常工作:

def permutations(items):

        n=len(items)
        if n==0:
            yield []
        else:
            for i in range(len(items)):
                for cc in permutations(items[:i]+items[i+1:]):
                    yield [items[i]] + cc


g = open("TextFIle.txt", "r")

x=raw_input('Input the letters: ')

for p in permutations(list(x)):

        q=''.join(p)
        for i in g:
            if i in q:
                print i

g.close()

2 个答案:

答案 0 :(得分:0)

问题是您在处理第一个排列时读取/使用整个文件。处理它的一种强制方法是每次循环时重置文件上的内部指针。

for p in permutations(list(x)):
    q = ''.join(p)
    for i in g:
        if i in q:
            print i
    g.seek(0)  # resetting the pointer back to the beginning of the file

根据您处理的内容,这可能没问题,但您最终会以指数循环结束(因此可能会非常慢)。

编辑我还注意到您的描述与代码完全匹配。你说"看看特定文本文件中是否存在任何这些字符串"但随后代码显示i in q,即检查文件的任何行是否是任何排列的子集(基本上是相反的方式)。

你能澄清一下你有什么样的排列以及你有什么样的文本行,因为如果你在文件中有一个空行,它会匹配任何输入(例如)。

听起来你想要更像以下内容:

raw_letters = raw_input('Input the letters: ')

# read out the entire contents of the file to search over
with open('TextFile.txt', 'r') as g:
    full_file = g.read()

# print each permutation that occurs somewhere in the file
for p in permutations(raw_letters):
    p_as_string = ''.join(p)
    if p_as_string in full_file:
        print(p_as_string)

编辑2所以我相信你需要一个拼字游戏求解器的行为。你有一个文件中的单词字典,人们需要能够输入他们的瓷砖来找到可能的单词。

我确定有更好的算法可以解决这个问题,但我会对相对简单的事情进行抨击。排列的想法是可靠的(并且考虑到问题是自然的),但它确实非常粗暴和低效。

关键的洞察力是,既然您要对所有排列进行迭代,那么您可以使用排序。在这种情况下,您可以按字母顺序对单词重新排序(因此try变为rty) - 让我们将其称为word_signature。您也可以以相同的方式订购瓷砖。然后,对于每个单词,您可以扫描以查看图块是否可以弥补。

# grab our word list as (original_word, word_signature) tuples
# you only have to do this once
words = []
with open('TextFIle.txt', 'r') as f:
    for word in f:
        word = word.strip()
        words.append((word, sorted(word)))


raw_letters = raw_input('Input the letters: ')
search_signature = sorted(raw_letters)

for word, word_signature in words:
    # these are the tiles we're searching over
    # pull it out like this so we can easily pop our way through
    remaining_search = list(reversed(search_signature))

    could_still_match = True
    found = []

    # this is going to look a bit horrible because you're sort of tracking
    # 2 cursors at the same time, incrementing them as you find matches, or not
    for letter in word_signature:

        while remaining_search:
            next_possible = remaining_search.pop()

            # either the possible letter is:
            # - the one we're looking for, that's great, move on to the next letter
            # - less than the one we're looking for, so we can skip it and look at
            #   another letter (by going through the next iteration of the while loop)
            # - greater than the one we're looking for, so this word is a non-match

            if next_possible == letter:
                found.append(next_possible)
                break

            if next_possible < letter:
                continue

            if next_possible > letter:
                could_still_match = False
                break

        # horrible little hack so we can break out of both loops
        if not could_still_match:
            break

    # if we found all the letters in the word then we have a match
    if len(found) == len(word_signature):
        print(word)

答案 1 :(得分:0)

在搜索之前阅读文件内容。

...
x=raw_input('Input the letters: ')
with open("TextFIle.txt", "r") as g:
    g = g.read()

for p in permutations(list(x)):
        q=''.join(p)
        if q in g:
            print q