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

321*_*321 1 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
Run Code Online (Sandbox Code Playgroud)

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

Too*_*ote 8

productitertools模块中获取正是您生成所有可能的双字母单词列表所需的内容.

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
Run Code Online (Sandbox Code Playgroud)

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

  • 用`itertools`很好的调用.在使用它时,重要的是要注意产品最终会产生26 ^ 2个条目,所以虽然这在内存(作为生成器函数)方面肯定是高效的,但它可能不是解决问题的最佳算法(特别是更长的时间)话). (2认同)

Jun*_*uxx 5

另一种方式,列表理解:

words = [x+y for x in alphabet for y in alphabet]
Run Code Online (Sandbox Code Playgroud)

或者自己不输入字母:

from string import ascii_lowercase as a
words = [x+y for x in a for y in a]
Run Code Online (Sandbox Code Playgroud)

让我们对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)
Run Code Online (Sandbox Code Playgroud)

结果:

返回列表的方法:
嵌套for循环:13.362
list(product)
:4.578 List comprehension:7.231

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

换句话说,itertools.product如果你真的需要一个完整的清单,一定要使用.然而,生成器更快并且需要更少的内存,并且可能就足够了.

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