【问题标题】:Calling different functions python调用不同的函数python
【发布时间】:2014-11-22 16:07:13
【问题描述】:

我需要调用我已经创建的各种不同的函数来解决下面的问题。我真的不确定如何对此进行编程以实现它。问题是..

在单词列表( str, str list ) 中找到两个单词变位词,其中输入参数应该 是一个字符串和一个字符串列表。输出应该是由两个组成的所有字符串的列表 用空格分隔的单词,这样这两个单词都在 str 列表中,并且组合 这两个词是str的变位词。

我期望的输出是:

wordlist = ('and','band','nor,'born')
find_two_word_anagrams_in_wordlist( "brandon", wordlist )
[’and born’, ’band nor’]

如何实现是:

  • 将两个单词字谜的列表初始化为空列表 []。
  • 调用 find partial anagrams in word list 以获取所有单词的列表 可以在单词列表中找到的 str 的部分字谜。
  • 然后,循环遍历所有这些部分字谜。对于每个 部分字谜部分字谜执行以下操作:
  • 从输入字符串str中去掉部分anag的字母,得到 一个字符串,rem 是去掉后剩下的字母 部分字谜;
  • 调用 find anagrams in word list on rem(和输入单词列表)到 获取剩余字母的字谜列表;
  • 对于剩余字母的每个 anagram rem anag,形成字符串 part anag + " " + rem anag 并将其添加到两个单词 anagrams 列表中。
  • 此时列表中的两个单词 anagrams 应该包含所有这两个 其中包含单词字谜,因此请从您的函数中返回该列表。

我已经创建的代码是:

def find_partial_anagrams_in_word_list(str1,str_list):
    partial_anagrams = []
    for word in str_list:
         if (partial_anagram(word, str1)):
             partial_anagrams.append(word)
    print(partial_anagrams)

def remove_letters(str1,str2):
    str2_list = list(str2)
    for char in str2:
        if char in str1:
             str2_list.remove(char)
    return "".join(str2_list)

def find_anagrams_in_word_list(str1,str_list):
    anagrams = []
    for word in str_list:
            if (anagram(str1,word)):
                anagrams.append(word)
                print(word)

任何分步帮助或输入将不胜感激。

【问题讨论】:

    标签: python string function python-3.3


    【解决方案1】:

    您有两个选择,您可以查看两个不同单词组合的字谜,或者您可以两两组合查看all中的字谜。

    选择权在你,这里我都实现了

    from itertools import combinations, combinations_with_replacement
    
    def ana2(s,wl):
        rl = []
        for w1, w2 in combinations(wl,2):
            w = w1+w2
            if len(w) != len(s): continue
            if sorted(w) == sorted(s): rl.append((w1, w2)
        return rl
    
    def ana2wr(s,wl):
        rl = []
        for w1, w2 in combinations_with_replacement(wl,2):
            w = w1+w2
            if len(w) != len(s): continue
            if sorted(w) == sorted(s): rl.append((w1, w2))
        return rl
    

    这是一些测试

    wl = ('and','band','nor','born')
    s1 = 'brandon'
    s2 = 'naddan
    
    print ana2(s1,wl)
    print ana2(s2,wl)
    
    print ana2wr(s1,wl)
    print ana2wr(s2,wl)
    

    产生以下输出

    [('and', 'born'), ('band', 'nor')]
    []
    [('and', 'born'), ('band', 'nor')]
    [('and', 'and')]
    

    【讨论】:

    • 您的参数是wl,但您使用的是wordlist。简而言之:ana = lambda s,wl,cnt=2: filter(lambda a, s=sorted(s):sorted(''.join(a))==s, combinations(wl, cnt))
    • 感谢您发现我的错误。实际上,我不确定我的答案是否适合 OP,它要求帮助他/她找到字谜的方式......
    • @gboffi 感谢您的回答,但在我的问题中我已经说过需要如何完成,我只是在努力做到这一点。
    • @alfiej12 在我对丹尼尔的回答中,我已经怀疑我的回答与您的 Q 的相关性。现在我很确定!你喜欢我完全删除我的答案吗?
    【解决方案2】:

    你写的

    我需要调用我已经创建的各种不同的函数 为了实现下面的问题

    def find_2_word_anagram(word, wordlist)            
        # Initialise a list two word anagrams to the empty list, [].
        analist = []
    
        # Call find partial anagrams in word list to get a list of all the
        # partial anagrams of str that can be found in the word list.
        candidates = find_partial_anagrams_in_word_list(word,wordlist)
    
    
        # Then, do a loop that runs over all these partial anagrams
        for candidate in candidates:
            # Remove the letters of part anag from the input string, str, to
            # get a string, rem that is the remaining letters after taking
            # away the partial anagram
            remaining = remove_letter(candidate,words)
            # Call find anagrams in word list on rem (and the input word list)
            # to get a list of anagrams of the remaining letters;
            matches = find_anagrams_in_word_list(remaining, wordlist)
            for other in matches:
                analist.append(" ".join(candidate,other))
    
        return analist
    

    注意

    1. 您仍然需要按照您的规范编写内部函数
    2. 当您编写一个返回的函数时,例如,一个单词列表,您必须返回一个单词列表,特别是我的意思是,仅打印函数中的匹配项是不够的
    3. 要查找字谜,成语sorted(w1)==sorted(w2) 就足够了,但是找到部分字谜的故事更复杂...
    4. 我冒昧地内联了您的一个函数,即计算余数的函数。
    5. 当您剥离 cmets,即逐字您的规范时,您的代码行数很少。

    后记

    仔细看看丹尼尔对我之前回答的评论,里面有很多...

    【讨论】:

    • 这就到了,谢谢!尽管它在“c for c in word if c not in Candidate”上的“for”上说无效语法
    • 我必须说三件事(1)你不应该有语法错误,我已经测试过了,它在语法上是正确的。 (2) 没关系,因为代码在语义上是错误的(代码删除了word 中也出现在candidate 中的任何字符的所有出现 )。 (3) 你必须写你的remove_letters。我已经编辑了我的答案以反映这些观点。
    • 另一个问题,你会发现partial_anagramremove_letters几乎相同的功能。这意味着您可以同时计算候选者和余数,并从单个函数中返回两者。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2017-06-22
    • 1970-01-01
    • 2022-11-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多