如何在python中使用函数生成2个字母的单词?

时间:2012-06-11 00:10:19

标签: python

嗨所以即时通讯使用python,我正在尝试创建一个函数,让我生成由2个字母组成的单词。我还想计算生成的字数实际上在字典中。

这是我到目前为止所做的:

alphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
            'p','q','r','s','t','u','v','w','x','y','z')
count1 = 0
text = " "

def find2LetterWords():
    for letter in alphabet:
        text += letter
        for letter in alphabet:
            text +=letter
    print text

这是我到目前为止编写的代码,我知道它不对。我只是在尝试。所以,如果你能帮助我,那就太好了。谢谢。

5 个答案:

答案 0 :(得分:8)

来自product模块的

itertools正是您生成所有可能的双字母单词列表所需的内容。

from itertools import product

alphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')

two_letter_words = product(alphabet, alphabet)

for word in two_letter_words:
    print word

要比较字典中的哪一个,您需要从其他地方获取

答案 1 :(得分:5)

另一种方式,列表理解:

words = [x+y for x in alphabet for y in alphabet]

或者自己不输入字母:

from string import ascii_lowercase as a
words = [x+y for x in a for y in a]

让我们对xvatar,Toote和我的答案进行比较:

from itertools import product
from string import ascii_lowercase as a
import timeit

def nestedFor():
    w = []
    for l1 in a:
        for l2 in a:
            word = l1+l2
            w.append(word)
    return w

def nestedForIter():
    w = []
    for l1 in a:
        for l2 in a:
            yield l1+l2

def withProduct():
    return product(a,a)

def listComp():
    return [x+y for x in a for y in a]

def generatorComp():
    return (x+y for x in a for y in a)

# return list
t1 =  timeit.Timer(stmt="nestedFor()",
                   setup = "from __main__ import nestedFor")
t2 = timeit.Timer(stmt="list(withProduct())",
                   setup = "from __main__ import withProduct")
t3 = timeit.Timer(stmt="listComp()",
                   setup = "from __main__ import listComp")

# return iterator
t4 = timeit.Timer(stmt="nestedForIter()",
                   setup = "from __main__ import nestedForIter")
t5 = timeit.Timer(stmt="withProduct()",
                   setup = "from __main__ import withProduct")
t6 = timeit.Timer(stmt="generatorComp()",
                   setup = "from __main__ import generatorComp")

n = 100000

print 'Methods returning lists:'
print "Nested for loops:   %.3f" % t1.timeit(n)
print "list(product):      %.3f" % t2.timeit(n)
print "List comprehension: %.3f\n" % t3.timeit(n)

print 'Methods returning iterators:'
print "Nested for iterator:     %.3f" % t4.timeit(n)
print "itertools.product:       %.3f" % t5.timeit(n)
print "Generator comprehension: %.3f\n" % t6.timeit(n)

结果:

  

返回列表的方法:
  嵌套for循环:13.362
  清单(产品):4.578
  列表理解:7.231

     

返回发电机的方法:
  嵌套迭代器:0.045
  itertools.product:0.212
  生成器理解:0.066

换句话说,如果您确实需要完整列表,请务必使用itertools.product。然而,生成器更快并且需要更少的内存,并且可能就足够了。

itertools.product作为迭代器的相对缓慢是意外的,考虑到the documentation表示等同于生成器表达式中的嵌套for循环。似乎有一些开销。

答案 2 :(得分:1)

def find2LetterWords():
    words = []
    for first in alphabet:
        for second in alphabet:
            new_word = first + second
            words.append(new_word)
    print words
    return words

答案 3 :(得分:1)

问题的第一部分已经得到了很好的回答,但这是第二部分。

  

我还想知道生成的单词实际上有多少   字典。

实际上这很容易。您知道单词列表中包含所有可能的组合。你知道字典键是唯一的;因此,两个字符长的键必须在单词列表中。您需要做的就是计算长度为2的键数。

counts = sum(len(k) == 2 for k in my_dict.iterkeys())

答案 4 :(得分:0)

根据评论编辑的答案:

def find2LetterWords():
     #this generates all possible 2-letter combos with a list comprehension
     words = [first + second for second in alphabet for first in alphabet]
     #create a new list with only those words that are in your_dictionary (a list)
     real_words = [word for word in words if word in your_dictionary]
     return real_words

如果你想要一个漂亮的衬垫,没有功能:

[word for word in [first + second for second in alphabet for first in alphabet] if word in your_dictionary]

显然,将your_dictionary替换为字典的名称。